https://www.acmicpc.net/problem/11720
내 답안 - 1
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int N = s.nextInt();
String num = s.next();
int total = 0;
for (int i=0; i<N; i++) {
total+=(int)num.charAt(i)-0x30;
}
System.out.println(total);
}
}
내 답안 - 2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int total = 0;
int N = s.nextInt();
String str = s.next();
for (int i=0; i<str.length(); i++) {
total+=Character.getNumericValue(str.charAt(i));
}
System.out.println(total);
}
}
char을 int로 변환하는 방법
'Coding Test > 백준' 카테고리의 다른 글
[백준 2440 - java] 별 찍기 - 3 (0) | 2021.11.16 |
---|---|
[백준 10809번 - java] 알파벳 찾기 (0) | 2021.11.14 |
[백준 8958번 - java ] OX퀴즈 (0) | 2021.11.13 |
[백준 11654번 - java] 아스키 코드 (0) | 2021.11.12 |
[백준 4344번 - java] 평균은 넘겠지 (0) | 2021.11.12 |