이 문제는 간단하다
말 그대로 반복문을 이용해서 모든 쌍의 최대공약수를 구하면서 계속 더해주면 된다!
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);
}
}
}
'백준온라인' 카테고리의 다른 글
(JAVA)백준온라인 1373번 - 2진수 8진수 (0) | 2023.08.23 |
---|---|
(JAVA) 백준온라인 17087번 - 숨바꼭질 6 (0) | 2023.08.17 |
(JAVA)백준 온라인 2004번 - 조합 0의 개수 (0) | 2023.08.12 |
(JAVA)백준온라인 1676번 - 팩토리얼 0의 개수 (0) | 2023.08.11 |
(JAVA)백준온라인 6588번 - 골드바흐의 추측 (0) | 2023.08.10 |