用辗转相除法求最大公约数!帮忙看一下,谢谢!

来源:百度知道 编辑:UC知道 时间:2024/06/08 16:32:24
我是这么写的,可是结果是0……麻烦帮我看一下哪里错了??谢谢!

#include<stdio.h>
main()
{
int c,a,b,k;
printf("please put tow numbers:");
scanf("%d%d",&a,&b);
if(a<b)
{k=a;a=b;b=k;}

while(c!=0)
{ c=a%b;
a=b;
b=c;
}
printf("the result is:%d",b);

}

把while循环修改一下就行了……
while(true)
{
c=a%b;
if(c==0)break;
a=b;
b=c;
}

辗转相除法...我是为这个来的...怀念一下自己的高中数学老师...

你这个...如果有输出,肯定是0嘛
看你的循环,只有c=0的时候才退出循环,要退出循环,c=0,退出循环之前,还把c的值赋给了b,再输出b?那当然是0了~

辗转相除可以考虑递归算法

#include<stdio.h>
int f(int a,int b)
{
if(a<b)
return f(b,a);
if(a%b==0)
return b;
else return f(b,a%b);
}

void main()
{
int x,y,n;
scanf("%d%d",&x,&y);
n=f(x,y);
printf("%d",n);
}

问题相当深奥。。。