UVA 11827 Maximum GCD

来源:互联网 发布:光盘数据恢复软件 编辑:程序博客网 时间:2024/05/23 22:49

Maximum GCD

 


Given the N integers, you have to find the maximum GCD (greatest common divisor) of every possiblepair 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) positiveintegers that you have to find the maximum of GCD.

OutputFor 
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

题意:给不超过一百个数,求两个数最大的gcd
水题,数据范围小,直接暴力过,但是WA了n发,这题输入的数据是真的傲娇,第一次见这样玩的,让人无语了,各种前导空格后导空格,= =,一不小心就错

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<string>#include<stack>#include<queue>#include<deque>#include<set>#include<map>#include<cmath>#include<vector>using namespace std;typedef long long ll;typedef pair<int, int> PII;#define pi acos(-1.0)#define eps 1e-10#define pf printf#define sf scanf#define lson rt<<1,l,m#define rson rt<<1|1,m+1,r#define e tree[rt]#define _s second#define _f first#define all(x) (x).begin,(x).end#define mem(i,a) memset(i,a,sizeof i)#define for0(i,a) for(int (i)=0;(i)<(a);(i)++)#define for1(i,a) for(int (i)=1;(i)<=(a);(i)++)#define mi ((l+r)>>1)const int inf=0x3f3f3f3f;char s[1110000];int T, a[1000];int gcd(int a,int b){    return !b?a:gcd(b,a%b);}int main(){    sf("%d",&T);    getchar();    while(T--)    {        mem(a,0);        int ans=0;        int p=0;        gets(s);//题目没说有多少组数据,只能输入字符串了,scanf("%s",s);这种输入不会读入空格不和题意        int d=strlen(s);        int tag=0;        for0(i,d)        {            if(s[i]!=' ')                a[p]=a[p]*10+s[i]-'0',tag=1;            else if(s[i]==' '&&tag)//如果前面是数字,当前为空格才令计数器加1                p++,tag=0;        }        if(!tag)//如果最后还有空格当前计数器减一            p--;        for(int i=0;i<p;i++)            for(int j=i+1;j<=p;j++)                ans=max(ans,gcd(a[i],a[j]));        pf("%d\n",ans);    }    return 0;}

原创粉丝点击