본문 바로가기
Coding Test/백준

[백준 11720번 - java] 숫자의 합

by olli2 2021. 11. 13.

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로 변환하는 방법

https://dotddori-prgmwh.tistory.com/86