Given the N integers, you have to find the maximum GCD (greatest common divisor) of every possible pair of these integers.
Input
The first line of input is an integer N (1 < N < 100) that determines the number of test cases. The following N lines are the N test cases. Each test case contains M (1 < M < 100) positive integers that you have to find the maximum of GCD.
Output
For each test case show the maximum GCD of every possible pair.
Sample Input
3
10 20 30 40
7 5 12
125 15 25
Sample Output
20
1
25
题意:求他们之间最大公因数的最大值,你直接每两个之间都求一下,然后取最大就行(暴力即可),注重一点就是得控制输入。
#include<stdio.h>
#include<string.h>
int fun(int x,int y)
{
if(y==0) return x;
else return fun(y,x%y);
}
int main()
{
int n;
scanf("%d",&n);
getchar();
while(n--)
{
char s[1000];
int a[1000];
gets(s);
int l=strlen(s);
int b=0,c=0,maxx=0;
for(int i=0; i<=l; i++)
{
if(s[i]!=' '&&i!=l)
b=b*10+s[i]-'0';
else if(b!=0)
{
a[c++]=b;
b=0;
}
}
for(int i=0; i<c; i++)
{
for(int j=i+1; j<c; j++)
{
if(maxx<fun(a[i],a[j]))
maxx=fun(a[i],a[j]);
}
}
printf("%d\n",maxx);
}
return 0;
}