比较坑的输入

给你N个整数, 拜托你帮我找找在这些所有的数字中组合可能的最大公约数 (greatest common divisor)

Input

第一行输入一个N (1 < N < 100) 表示样例的数量。
接下来N行每行有 M (1 < M < 100) 个正整数,请寻找其中的最大公约数.(M不需要你输入)

Output

输出每一行的最大公约数

Input

2
7 5 12
125 15 25

Output

1
25

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1e6+100;
int gcd(int a,int b){
    if(b==0){
        return a;
    }
    else{
        return gcd(b,a%b);
    }
}
int a[maxn];
int main(){
    int t;
    char c;
    cin>>t;
    while(t--){
        int p=0;
        int x;
        while(scanf("%d",&a[p++])){
             while((c=getchar())==' ');//读空格
                 ungetc(c,stdin);//将一个字符返回到输入流中,即将数字放在数组num中
                if(c=='
') break;//换行退出
        }
        int ma=0;
        for(int i=0;i<p-1;i++){
            for(int j=i+1;j<p;j++){
                ma=max(ma,gcd(a[i],a[j]));
            }
        }
        cout<<ma<<endl;
    }
}
原文地址:https://www.cnblogs.com/lipu123/p/13912127.html