day3 백준 11720번 : 숫자의 합 [Java]

문제출처

https://www.acmicpc.net/problem/11720

 

11720번: 숫자의 합

첫째 줄에 숫자의 개수 N (1 ≤ N ≤ 100)이 주어진다. 둘째 줄에 숫자 N개가 공백없이 주어진다.

www.acmicpc.net

예제에 있는 큰수들 때문에 자바에 BigInteger라는게 있는지 처음 알게됐다.

import java.util.Scanner;
import java.math.BigInteger;
public class Q11720 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		BigInteger input = new BigInteger(sc.next());
		
		BigInteger total = new BigInteger("0");
		for(int i = 0; i < n; i++) {
			total = total.add(input.remainder(BigInteger.valueOf(10)));
			input = input.divide(BigInteger.valueOf(10));
			
		}
		System.out.println(total);
		sc.close();
	}

}

이거말고 charAt를 이용해서 푸는 방법도 있더라 

myoskin