超级恶心的输入

题目描述:
给你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 <cstring>
#include <cstdio>
using namespace std;
int gcd(int a,int b){
    return b ? gcd(b,a%b) : a;
}
int main(){
    int t;
    int a[105];
    char c;
    scanf("%d",&t);
    while(getchar() != '\n');
    while(t--)
	{
        int cnt = 0;
        while((c = getchar()) != '\n')
		{
            if(c >= '0' && c <= '9')
			{
                ungetc(c,stdin);	//就是这玩意儿,一个字符一个字符接受,以数字形式吐出来
                scanf("%d",&a[cnt++]);
            }
        }
        int maxx = 0;
        for(int i = 0; i < cnt-1; i++)	//双重循环直接暴力。
		{
            for(int j = i+1; j < cnt; j++)
			{
                int d = gcd(a[i],a[j]);
                if(d > maxx) maxx = d;
            }
        }
        printf("%d\n",maxx);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_46687179/article/details/105973406