HDU2504又见GCD

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=2504

在不知道这gcd函数时写的代码

#include<stdio.h>
int main()
{
int n,a,b,i,t1,t2,m;
scanf("%d",&n);
while(n--)
{
scanf("%d%d",&a,&b);
if(a<b)
a=a^b^(b=a);
for(i=2;;i++)
{
if(a%(i*b)!=0)
{
t1=a/b;
t2=i;
if(t1<t2)
t1=t1^t2^(t2=t1);
while(t2)
{
m=t1%t2;
t1=t2;
t2=m;
}
if(t1==1)
{
printf("%d\n",i*b);
break;
}
}
}
}
return 0;
}

感觉吗,没水平!

下面是gcd函数的代码(gcd是用来求最大公约数的)

#include<stdio.h>

int gcd(int a,int b)
{
if(a==0) return b;
if(b==0) return a;
return gcd(b,a%b);
}

int main(void)
{
int n,a,b;
scanf("%d",&n);
while(n--)
{
scanf("%d%d",&a,&b);
int i,c;
for(i=2;;i++)
{
c=i*b;
if(gcd(a,c)==b)
{
printf("%d\n",c);
break;
}
}
}
return 0;
}

总体感觉这题还是比较水的。

原文地址:https://www.cnblogs.com/liudehao/p/3957368.html