题意
首先有给定一个 T,T组数据,然后给你一组数。
求出其中两两最大公约数中最大的值。
样例
Sample Input
3
10 20 30 40
7 5 12
125 15 25
Sample Output
20
1
25
AC代码
package rr;
import java.util.Scanner;
public class test {
static int gcd(int a, int b) {
int c;
if (b == 0)
c = a;
else
c = gcd(b, a % b);
return c;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t, i, j, max, count;
t = scan.nextInt();
scan.nextLine();
i = 0;
while (t-- != 0) {
//输入一个字符串,以空格隔开转化成字符串数组,再将字符串数组转化为整型数组
String inputString = scan.nextLine();
String stringArray[] = inputString.split(" ");
int a[] = new int[stringArray.length];
for (i = 0; i < stringArray.length; i++) {
a[i] = Integer.parseInt(stringArray[i]);
}
max = 0;
count = i;//将i解放出来,让他用到后面的循环中去
//找出所有两个数的最大公约数,再取最大值
for (i = 0; i < count; i++) {
for (j = i + 1; j < count; j++) {
int temp = gcd(a[i], a[j]);
if (max < temp)
max = temp;
}
}
System.out.println(max);
}
}
}
题源:onlinejudge.org/index.php?o…