UVA 11827 Maximum GCD

F - Maximum GCD

Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Given the N integers, you have to nd the maximum GCD (greatest common divisor) of every possible
pair of these integers.
Input
The rst 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 nd 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

题目很水 暴力就能过 难点在如何输入没有停止的数

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <sstream>
using namespace std;
int gcd (int a,int b)
{
    if(b==0)
    return a;
    else
    return gcd(b,a%b);
}
int main()
{
    int i,j,n,t,a[105];
    cin>>t;
    getchar();
    while(t--)
    {
        //getchar();
        memset(a,0,sizeof(a));
        string str;
        getline(cin,str);
        stringstream stream(str);
        int n=0;
        while(stream>>a[n])
        {
            n++;
        }
        int ans=1;
        for(i=0;i<n;i++)
        for(j=i+1;j<n;j++)
        ans=max(gcd(a[i],a[j]),ans);
        cout<<ans<<endl;
    }
    return 0;
}

 

posted @ 2016-03-20 23:16  Ritchie丶  阅读(232)  评论(0编辑  收藏  举报