백준온라인
(JAVA)백준온라인 9613번 - GCD합
진드윽이
2023. 8. 17. 11:28
이 문제는 간단하다
말 그대로 반복문을 이용해서 모든 쌍의 최대공약수를 구하면서 계속 더해주면 된다!
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int i = 0; i<t; i++){
long result = 0;
int[] ary = new int[sc.nextInt()];
for(int j =0; j<ary.length; j++){
ary[j] = sc.nextInt();
}
for(int j = 0; j < ary.length - 1; j++){
for(int k = j+1; k<ary.length; k++){
int a = ary[j];
int b = ary[k];
int GCD = gcd(Math.max(a,b), Math.min(a,b));
result += GCD;
}
}
System.out.println(result);
}
}
public static int gcd(int x, int y) {
int a = x % y;
if (a == 0) {
return y;
} else {
return gcd(y, a);
}
}
}