Maximum GCD
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
题的大意是在所给出n个数任意选两个数,求这两个数的最大公因数,最后求最大的最大公因数。
这道题是一个水题但是输入输出是最大的坑,比如一次性输入多个空格啦。。。。。。
ac代码
#include <iostream>
#include <string>
#include <cmath>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int gcd(int x,int y)
{
if(x==0)
return y;
else
return gcd(y%x,x);
}
int main()
{
int i, j;
int a;
cin >> a;
getchar();
while(a--)
{
int ans[101], cns = 0, maxn = 1;
char c;
while((c=getchar())!='\n')
{
if (c >= '0' && c <= '9') {
ungetc(c,stdin);
scanf("%d",&ans[cns++]);
}
}
for(i = 0; i < cns; i++)
for(j = 0; j < cns; j++)
{
for(i = j + 1; i < cns; i++)
{
maxn=max(maxn,gcd(ans[i],ans[j]));
}
}
cout << maxn <<endl;
}
return 0;
}