Maximum GCD

来源:互联网 发布:js 判断radio选中 编辑:程序博客网 时间:2024/06/12 00:57

Given the
Nintegers, you have to nd the maximum GCD (greatest common divisor) of every possiblepair of these integers.
Input
The rst line of input is an integerN(1

这里写代码片#include<iostream>#include<algorithm>#include<cstring>#include<string>using namespace std;int gcd(int a,int b){    return b==0?a:gcd(b,a%b);}int main(){    int t;    scanf("%d",&t);    getchar();//避免跳行被读入    while(t--)    {        int a[110],sum=0,len=0,maxn=0;        char s;        while(1)        {            scanf("%c",&s);            if(s=='\n')            break;            if(s>='0'&&s<='9')            sum=sum*10+s-'0';            else            {                if(sum==0)//空格连续                continue;                if(sum!=0)                a[len++]=sum;                sum=0;            }        }        if(sum!=0)        a[len++]=sum;        for(int i=0;i<len;i++)        {            for(int j=i+1;j<len;j++)            {                maxn=max(maxn,gcd(a[i],a[j]));            }        }        printf("%d\n",maxn);    }    return 0; } 
0 0
原创粉丝点击