알고리즘/기초수학
[java 백준] 실버 3/ 9613번 GCD 합
Meaning_
2021. 9. 4. 17:01
728x90
반응형
https://www.acmicpc.net/problem/9613
9613번: GCD 합
첫째 줄에 테스트 케이스의 개수 t (1 ≤ t ≤ 100)이 주어진다. 각 테스트 케이스는 한 줄로 이루어져 있다. 각 테스트 케이스는 수의 개수 n (1 < n ≤ 100)가 주어지고, 다음에는 n개의 수가 주어진
www.acmicpc.net
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
27
28
29
30
31
32
33
34
35
36
37
38
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static long gcd(long a, long b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int num = Integer.parseInt(br.readLine());
long[] totals = new long[num];
for (int i = 0; i < num; i++) {
long total = 0;
String s = br.readLine();
String[] arr = s.split(" ");
for (int j = 1; j <= arr.length - 2; j++) {
for (int k = j + 1; k <= arr.length - 1; k++) {
total += gcd(Long.parseLong(arr[j]), Long.parseLong(arr[k]));
}
}
totals[i] = total;
}
for (int i = 0; i < num; i++) {
System.out.println(totals[i]);
}
}
}
|
cs |
total변수를 long으로 두는게 핵심이다!!
728x90
반응형