Maximum GCD UVA

来源:互联网 发布:淘宝网店的swot分析 编辑:程序博客网 时间:2024/06/06 08:43

Given the N integers, you have to find the maximum GCD (greatest common divisor) of every possible
pair of these integers.
Input
The first line of input is an integer N (1 < N < 100) that determines the number of test cases.
The following N lines are the N test cases. Each test case contains M (1 < M < 100) positive
integers that you have to find the maximum of GCD.
Output
For each test case show the maximum GCD of every possible pair.
Sample Input
3
10 20 30 40
7 5 12
125 15 25
Sample Output
20
1
25

暴力的gcd ,输入很少见,一直卡在这。。。

// 这个题真是醉了,居然卡在输入上#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<queue>#include<algorithm>#define ll long longusing namespace std;int gcd(int a,int b){    if( a < b)    {        int tmp = a;        a = b;        b = tmp;    }    return b==0? a : gcd(b,a%b);    }int a[150];int num[150];int main(){    int n;    scanf("%d",&n);    getchar();     // 吸收回车     for(int i=0;i<n;i++)    {        int flag = 1;        int cnt = 0;        char ch;        while( ( ch = getchar() ) != '\n')        {            if( '0' <= ch && ch <= '9')                {                    ungetc(ch,stdin);     // 如果是数字,将它退流                     scanf("%d",&a[cnt++]);                }        }        int maxn;        maxn = -1;        int tmp;        for(int j = 0; j < cnt; j ++)            for(int k = 0; k < j; k ++)                {                    tmp = gcd( a[j], a[k] );                    if( tmp > maxn)                        maxn = tmp;                }        printf("%d\n",maxn);    }    return 0;}
0 0
原创粉丝点击