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를 이용해서 푸는 방법도 있더라
'문제풀이 > 코딩테스트' 카테고리의 다른 글
day6 백준 1011번 : Fly me to the Alpha Centauri [Java] (0) | 2020.09.03 |
---|---|
day5 백준 10250번 : ACM 호텔 [Java] (0) | 2020.09.01 |
day4 백준 2869번 : 달팽이는 올라가고 싶다 [Java] (0) | 2020.08.31 |
day2 백준 2839번 : 설탕 배달 [Java] (0) | 2020.08.27 |
day1 백준 10809번 : 알파벳 찾기 [Python] (0) | 2020.08.24 |