[BaekJoon] #9085 - 더하기 [Java][C++]
[BaekJoon] #9085 - 더하기 [Java][C++]
1. 아이디어
한 줄씩 주어진 각 수들을 전부 더하기만 하면 된다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(T \times N)$ | $O(1)$ |
($T$ = 테스트 케이스 수, $N$ = 케이스당 n의 최댓값)
3. 코드
풀이 [Java][C++]
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
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();
StringTokenizer st;
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
int n = Integer.parseInt(br.readLine());
int sum = 0;
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';
}
}
This post is licensed under CC BY 4.0 by the author.