[Snow-ball]프로그래밍(컴퓨터)/Algorithm Training
[알고리즘 트레이닝] 백준 알고리즘 11720번 : 숫자의 합 [JAVA]
Snow-ball
2021. 6. 15. 12:54
반응형
DAY ONE 을 잊지말자!!
문제
N개의 숫자가 공백 없이 쓰여있다. 이 숫자를 모두 합해서 출력하는 프로그램을 작성하시오.
입력
첫째 줄에 숫자의 개수 N (1 ≤ N ≤ 100)이 주어진다. 둘째 줄에 숫자 N개가 공백없이 주어진다.
출력
입력으로 주어진 숫자 N개의 합을 출력한다.
풀이 :
1번.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
import java.util.Scanner;
public class Algorithm_11720 {
public static void main(String[] args) {
// N개의 숫자가 공백 없이 쓰여있다. 이 숫자를 모두 합해서 출력하는 프로그램을 작성하시오.
// 첫째 줄에 숫자의 개수 N (1 ≤ N ≤ 100)이 주어진다. 둘째 줄에 숫자 N개가 공백없이 주어진다.
int totalNum = 0;
Scanner scan = new Scanner(System.in);
Long in = scan.nextLong();
if (in > 0 && in <= 100) {
Scanner scan2 = new Scanner(System.in);
Long inputNum = scan2.nextLong();
while(inputNum != 0) {
for (int i=0; i < in; i++) {
totalNum += inputNum % 10;
inputNum /= 10;
}
}
}
System.out.println("마지막 totalNum : " + totalNum);
}
}
|
cs |
2번.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.company;
import java.util.Scanner;
public class Algorithm_11720_2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
String number = scan.next();
scan.close();
int totalNum = 0;
for (int i=0; i < N; i++) {
totalNum += number.charAt(i)-'0';
}
System.out.println("totalNum : " + totalNum);
}
}
|
cs |
3번.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Algorithm_11720_3 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("BufferedReader : \n");
// BufferedReader의 readLine() 을 사용하면 데이터를 라인 단위로 읽을 수 있음
// readLine ㅎ마수의 리턴 값은 STring으로 고정
br.readLine();
System.out.print("br.readLine : ");
int sum = 0;
for (byte value : br.readLine().getBytes()) {
sum += (value - '0');
}
System.out.print(sum);
}
}
|
cs |
베타존 : 네이버쇼핑 스마트스토어
나를 꾸미다 - 인테리어소품 베타존
smartstore.naver.com
반응형