uva 11827 Maximum GCD(输入技巧)

题意:对于给定的一组数,求该组数中两两gcd的最大值;

思路:简单gcd,亮点在于每组数的个数并不提供,因此需要在读入是做出判断;

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define eps 1e-5
int t,i,j,k,num[50010],b,flag,shu,mm;
int gcd(int a,int b)
{
    int c;
    c=a%b;
    while(c)
    {
        a=b;b=c;c=a%b;
    }
    return b;
}
int main()
{
    char c;
    while(scanf("%d",&t)!=EOF)
    {
        while(t--)
        {
            memset(num,0,sizeof(num));
            shu=0;mm=0;
            while(1)
            {
                scanf("%d",&num[shu++]);
                 while((c=getchar())==' ');//读空格
                 ungetc(c,stdin);//将一个字符返回到输入流中,即将数字放在数组num中
                if(c=='\n') break;//换行退出
            }
            shu--;
            for(i=0;i<=shu;i++)
            {
                for(j=i+1;j<=shu;j++)
                {

                    if(mm<gcd(num[i],num[j])) mm=gcd(num[i],num[j]);
                }
            }
            printf("%d\n",mm);

        }
    }
    return 0;
}

 

posted on 2015-04-05 11:38  大树置林  阅读(262)  评论(0编辑  收藏  举报

导航