UVA11827(GCD)鬼输入

    题目:给你一组数,求出其中两两最大公约数中最大的值

      解析:数论,小数据直接枚举。

    坑点:输入,可能有多余空格,TL问题

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=1e2+10;
int a[maxn];
int maxx=-1;
int gcd(int a,int b)
{
    return b?gcd(b,a%b):a;      ///
}
int main()
{
    int t;char ch;
    scanf("%d",&t);
    getchar();
    while(t--)
    {
        char s[maxn];
        gets(s);
        int to=0,cnt=0;
        int num[maxn];
        for(int i=0;s[i]!='\0';i++)
        {
            if(s[i]==' ')
            {
                num[cnt++]=to;
                to=0;
            }
            else
                to=to*10+s[i]-'0';
        }
        if(to)
            num[cnt++]=to;
        maxx=-1;
        for(int i=0;i<cnt;i++)
        {
            for(int j=i+1;j<cnt;j++)
                {
                    maxx=max(maxx,gcd(num[i],num[j]));
                }
        }
        cout<<maxx<<endl;
    }
}

 

posted @ 2020-03-09 16:31  liyexin  阅读(131)  评论(0编辑  收藏  举报