-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsum_GCD.java
More file actions
41 lines (34 loc) · 818 Bytes
/
Copy pathsum_GCD.java
File metadata and controls
41 lines (34 loc) · 818 Bytes
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
39
40
41
package math;
import java.util.ArrayList;
import java.util.Scanner;
public class sum_GCD {
public static void main(String[] args) {
// #9613_GCD 합
Scanner sc = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<>();
int x = sc.nextInt();
for (int i = 0; i < x; i++) {
int n = sc.nextInt();
for (int j = 0; j < n; j++) {// 두번째 테스트케이스
list.add(sc.nextInt()); // 숫자 넣어주기
}
long a = 0;
for (int z = 0; z < list.size() - 1; z++) {
for (int c = z + 1; c < list.size(); c++) {
a += gcd(list.get(z), list.get(c));
}
}
System.out.println(a);
list.clear();
}
}
// 최대공약수 구하기
public static int gcd(int a, int b) {
if(b == 0) {
return a;
}
else {
return gcd(b, a%b);
}
}
}