[JAVA | 백준 11720] 숫자의 합
· 테크· Java
Java
백준 11720번 문항 www.acmicpc.net/problem/11720
문제
N개의 숫자가 공백 없이 쓰여있다. 이 숫자를 모두 합해서 출력하는 프로그램을 작성하시오.
입력
첫째 줄에 숫자의 개수 N (1 ≤ N ≤ 100)이 주어진다. 둘째 줄에 숫자 N개가 공백없이 주어진다.
출력
입력으로 주어진 숫자 N개의 합을 출력한다.
풀이
char 배열로 받은 수를 for문을 활용해서 모두 더한다.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import java.util.Scanner;
public class string_11720 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String cnt = sc.nextLine();
String number = sc.nextLine();
int sum = 0;
char[] chArr = number.toCharArray();
for (int i = 0; i < Integer.parseInt(cnt); i++) {
sum += chArr[i] - '0';
}
System.out.println(sum);
sc.close();
}
}
|
cs |
Comments
No comments yet. Be the first!
319 posts in 테크
- 368Supabase 프로젝트 복사하기 (Restore to a New Project)NEW
- 341Migrating from Permanent Access Tokens to Token Exchange — Why Order Matters
- 326Startup & Product Glossary: Terms Every Solo Founder Should Know
- 325Context Management — How I Do It Now
- 324Claude Code Routines vs Cowork Schedule — What's the Difference?
1–5 / 319