苍海的王子玛纳霏 bt:c语言高手来帮帮忙

来源:百度文库 编辑:高校问答 时间:2024/04/29 10:46:50
用调用函数的方法,输入a,b求他们的最大公约数,和最小公倍数,谢谢各位大哥了.

比较a和b的大小

然后保存到r

double temp;
int r;
if(a>b) r=b;
else r=a;
for(int i=r; i>0; i--){
if(modf((double)(a/i), &temp) == 0 && modf(double)(b/i), &temp) == 0){
printf("最大公约数为 %i", i);
break;
}
}

for(int i=r; i<r*r; i++){
if(modf((double)(i/a), &temp) == 0 && modf((double)(i/b), &temp) == 0)){
printf("最小公倍数为 %i", i);
break;
}
}

你连这个都不会。在考二级吧。回去好好看看书吧

你是指递归函数调用吧
int gcd(int a,int b)
{
if(a==b)
return a;
else
if(a==1 || b==1)
return a*b;
else
if(a>b)
return gcd(a%b,b);
}

算法:辗转相除法