Post

[BaekJoon] #9085 - 더하기 [Java][C++][Python]

[BaekJoon] #9085 - 더하기 [Java][C++][Python]

문제 링크


1. 아이디어

테스트 케이스마다 $N$개의 자연수를 더해 한 줄에 하나씩 출력한다.


2. 코드

풀이 [Java][C++][Python]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();

        int t = Integer.parseInt(br.readLine());
        while (t-- > 0) {
            int n = Integer.parseInt(br.readLine());

            int sum = 0;
            StringTokenizer st = new StringTokenizer(br.readLine());
            while (n-- > 0) {
                sum += Integer.parseInt(st.nextToken());
            }

            sb.append(sum).append("\n");
        }

        System.out.print(sb);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;

    while (t--) {
        int n;
        cin >> n;

        int sum = 0;
        while (n--) {
            int x;
            cin >> x;
            sum += x;
        }

        cout << sum << '\n';
    }
}
1
2
3
4
5
6
7
8
import sys

input = sys.stdin.readline

t = int(input())
for _ in range(t):
    input()
    print(sum(map(int, input().split())))

개수를 알려 주는 줄은 합을 구하는 데 필요 없어 읽고 버렸다.


This post is licensed under CC BY 4.0 by the author.