알고리즘 및 자료구조/문제
백준알고리즘 11720번 숫자의 합
ktko
2018. 5. 1. 11:59
백준알고리즘
https://www.acmicpc.net/problem/11720
BigInteger을 써서 별짓 다해봤는데 아니였다. 간단하게 숫자를 String로 받아서 길이만큼 charAt() 메서드로 char을 받는다
근데 char에서 int로 컨버터 하는것을 알지 못해 인터넷으로 확인해보았는데
String.charAt(0) - '0' 이런 식으로 사용하면 int형으로 변환이 되었다. substring으로 Integer.parseInt()로 사용해도 될 것 같다.
import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int length = Integer.parseInt(scan.nextLine().trim()); String value = scan.nextLine().trim(); int result = 0; for(int i=0; i < length; i++) { result += value.charAt(i) - '0'; } System.out.println(result); } }