uva 11827 Maximum GCD ungetc函数的使用

来源:互联网 发布:淘宝交易管理插件 编辑:程序博客网 时间:2024/06/05 02:50
  1. 数据量小,可直接枚举。
  2. 要注意最后EOF的处理。
  3. ungetc可以将字符返回缓冲区。

题目链接:http://acm.hust.edu.cn/vjudge/problem/18915

#include<cstdio>#include<iostream>#include<sstream>#include<cstdlib>#include<cmath>#include<cctype>#include<string>#include<cstring>#include<algorithm>#include<stack>#include<queue>#include<set>#include<map>#include<ctime>#include<vector>#include<fstream>#include<list>using namespace std;#define ms(s) memset(s,0,sizeof(s))typedef unsigned long long ULL;typedef long long LL;const int INF = 0x3fffffff;LL gcd(LL a, LL b){    while(b){        LL t = a;        a = b;        b = t%b;    }    return a;}int main(){//    freopen("F:\\input.txt","r",stdin);//    freopen("F:\\output.txt","w",stdout);//    ios::sync_with_stdio(false);    int n;    char t;    scanf("%d",&n);    getchar();    LL num[105];    int idx;    LL maxs;    for(int i = 1; i <= n; ++i){        idx = 0;        maxs = 1LL;        scanf("%lld",&num[idx++]);        while(~scanf("%c",&t)){            if(t >= '0' && t <= '9'){                ungetc(t,stdin);                scanf("%lld",&num[idx++]);            }            if(t == '\n')                break;        }        for(int j = 0; j < idx-1; ++j){            for(int k = j+1; k < idx; ++k){                LL gcdd = gcd(num[j],num[k]);                if(gcdd > maxs)                    maxs = gcdd;            }        }        printf("%lld\n",maxs);    }    return 0;}
1 0