0
点赞
收藏
分享

微信扫一扫

UVA11827(GCD)鬼输入

海滨公园 2022-09-26 阅读 19

    题目:给你一组数,求出其中两两最大公约数中最大的值

解析:数论,小数据直接枚举。

    坑点:输入,可能有多余空格,TL问题

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=1e2+10;
int a[maxn];
int maxx=-1;
int gcd(int a,int b)
{
return b?gcd(b,a%b):a;      ///
}
int main()
{
int t;char ch;
scanf("%d",&t);
getchar();
while(t--)
{
char s[maxn];
gets(s);
int to=0,cnt=0;
int num[maxn];
for(int i=0;s[i]!='\0';i++)
{
if(s[i]==' ')
{
num[cnt++]=to;
to=0;
}
else
to=to*10+s[i]-'0';
}
if(to)
num[cnt++]=to;
maxx=-1;
for(int i=0;i<cnt;i++)
{
for(int j=i+1;j<cnt;j++)
{
maxx=max(maxx,gcd(num[i],num[j]));
}
}
cout<<maxx<<endl;
}
}

 



举报

相关推荐

0 条评论