๐Ÿ•ธ Algorithm/๐Ÿ•ธ ๋ฐฑ์ค€ BaekJoon

[๋ฐฑ์ค€][java][11720][์ˆซ์ž์˜ ํ•ฉ]

๋‰ด์ด NUEY 2024. 1. 26. 22:39
๋ฐ˜์‘ํ˜•

import java.util.Scanner;

// 11720	์ˆซ์ž์˜ ํ•ฉ
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		
		int num = in.nextInt();
		int sum = 0;
		
		String str = in.next();
		in.close();
		
		for (int i = 0; i < num; i++) {
			sum += str.charAt(i) - '0';
		}
		
		System.out.print(sum);
	}
}
  • ๋ฌธ์ž๋กœ ์ž…๋ ฅ๋ฐ›์•„ chatAt()์œผ๋กœ ๊ฐ ์ˆซ์ž๋ฅผ ๋”ํ•ด์ฃผ๋Š”๋ฐ
  • ์ด๋•Œ char๋ฅผ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋ ค๋ฉด -'0'์„ ํ•ด์ค๋‹ˆ๋‹ค.
๋ฐ˜์‘ํ˜•