#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b,r
scanf("%d %d", &a, &b)
while(b!=0)//当其中一个数为0,另一个数就是两数的最大公约数
{
r = a%b
a = b
b = r
}
printf("Greatest Common Divisor: %d\n", a)
system("pause")
}
运行结果:
按照你的改了一下
#include <stdio.h>int gcd(int x,int y)
{
int i
int max,min
(x>y)?(max=x,min=y):(max=y,min=x)
if(i=max%min!=0)
do{
i=min
min=max%min
max=i
}while(min!=0)
return max
}
int main()
{
int a,b
scanf("%d%d",&a,&b)
printf("%d\n",gcd(a,b))
return 0
}
再给你一个精简版,二者实质是一样的
#include <stdio.h>int gcd(int x,int y)
{
if(y==0) return x
return gcd(y,x%y)
}
int main()
{
int a,b
scanf("%d%d",&a,&b)
printf("%d\n",gcd(a,b))
return 0
}