์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ/BOJ_Java

[BOJ/Step7] 11720 : ์ˆซ์ž์˜ ํ•ฉ (JAVA)

NaNaRin๐Ÿ™ƒ 2021. 1. 11. 12:17

www.acmicpc.net/problem/11720

 

11720๋ฒˆ: ์ˆซ์ž์˜ ํ•ฉ

์ฒซ์งธ ์ค„์— ์ˆซ์ž์˜ ๊ฐœ์ˆ˜ N (1 ≤ N ≤ 100)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์— ์ˆซ์ž N๊ฐœ๊ฐ€ ๊ณต๋ฐฑ์—†์ด ์ฃผ์–ด์ง„๋‹ค.

www.acmicpc.net


๋ฌธ์ œ

N๊ฐœ์˜ ์ˆซ์ž๊ฐ€ ๊ณต๋ฐฑ ์—†์ด ์“ฐ์—ฌ์žˆ๋‹ค. ์ด ์ˆซ์ž๋ฅผ ๋ชจ๋‘ ํ•ฉํ•ด์„œ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ˆซ์ž์˜ ๊ฐœ์ˆ˜ N (1 ≤ N ≤ 100)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์— ์ˆซ์ž N๊ฐœ๊ฐ€ ๊ณต๋ฐฑ์—†์ด ์ฃผ์–ด์ง„๋‹ค.

 

์ถœ๋ ฅ

์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ์ˆซ์ž N๊ฐœ์˜ ํ•ฉ์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

์˜ˆ์ œ ์ž…๋ ฅ 1

1

1

 

์˜ˆ์ œ ์ถœ๋ ฅ 1

1

 

์˜ˆ์ œ ์ž…๋ ฅ 2

5

54321

 

์˜ˆ์ œ ์ถœ๋ ฅ 2

15

 

์˜ˆ์ œ ์ž…๋ ฅ 3

25

7000000000000000000000000

 

์˜ˆ์ œ ์ถœ๋ ฅ 3

7

 

์˜ˆ์ œ ์ž…๋ ฅ 4

11

10987654321

 

์˜ˆ์ œ ์ถœ๋ ฅ 4

46


ํ’€์ด

1. ์ˆซ์ž์˜ ๊ฐœ์ˆ˜ num์™€ ๋ฌธ์ž์—ด para๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.

2. para๋ฅผ ํ•œ๊ธ€์ž์”ฉ ๋ถˆ๋Ÿฌ์™€  int๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ total์— ๋”ํ•œ๋‹ค.

    => charAt() ๋ฉ”์†Œ๋“œ, getNumericValue() ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ

3. total์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

import java.util.Scanner;

public class B11720 {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int num = sc.nextInt();
		String para = sc.next();
		int total = 0;

		for(int i = 0; i < num; i++) {
			total += Character.getNumericValue(para.charAt(i));		
		}
		
		System.out.println(total);
	}
}