辗转相除求最大公约数
我说过会这样写的#include<stdio.h>
int gcd(int a,int b);
int main()
{
int num1=0,num2=0;
while(scanf("%d%d",&num1,&num2) !=EOF)
{
if(num1==0||num2==0)break;
int result;
result=gcd(num1,num2);
printf("%d\n",result);
}
return 0;
}
int gcd(int a,int b)
{
int i=0;
while(1)
{
i=a%b;
if(i==0)
return b;
else
{
a=b;
b=i;
}
}
return 0;
}
但是
老师给的模板是这样的
#include<stdio.h>
unsigned gcd(unsignedx,unsigned y)
{
}
int main()
{
int a,b;
while(scanf("%d%d",&a,&b),a||b)
printf("%d\n",gcd(a,b));
}
这里面应该怎么写呢= = 本帖最后由 yuxijian2020 于 2021-7-8 15:06 编辑
你把你自己写的合并一下不就行了么...
#include <stdio.h>
unsigned gcd(unsigned x, unsigned y)
{
if (x == 0 || y == 0)
return 0;
int i = 0;
while (1)
{
i = x % y;
if (i == 0)
return y;
x = y;
y = i;
}
return 0;
}
int main(int argc, char* argv[])
{
int a, b;
while (scanf_s("%d%d", &a, &b), a || b)
printf_s("%d\n", gcd(a, b));
return 0;
}
页:
[1]