【摘要】 问题说明:这个问题相信大家一定不会陌生吧,但是你会用代码实现出来吗,那就一起来研究研究吧!
1>自己瞎摸索的代码(仅供参考)
#include <stdio.h>
int main()
{
int a, b , x;
printf(“请输入两个数:”);
scanf(“%d%d”,&a , &b);
while(a != b){
if(a>b){
x = a-…
问题说明:这个问题相信大家一定不会陌生吧,但是你会用代码实现出来吗,那就一起来研究研究吧!
1>自己瞎摸索的代码(仅供参考)
#include <stdio.h>
int main()
{
int a, b , x;
printf("请输入两个数:");
scanf("%d%d",&a , &b);
while(a != b){
if(a>b){
x = a-b;
while(x>b){ x -= b;
}
}else{
x = b-a;
while(x>a){ x -= a;
}
}
printf("%d和%d的最大公约数为:%d",a,b,x);
return 0;
}
}
2>while循环:
#include <stdio.h>
int main()
{
int a , b , s;
printf("请输入两个整数:");
scanf("%d%d",&a,&b);
printf("a = %d , b = %d \n",a , b);
s = a;
while(a%s!=0 || b%s!=0){
s--;
}
printf("最大公约数为:%d \n",s);
}
3>while循环嵌套if语句:
#include <stdio.h>
int main()
{
int a , b , s;
printf("请输入两个整数:");
scanf("%d%d",&a,&b);
printf("a = %d , b = %d \n",a , b);
s = a;
while(s>=1){
if(a%s==0 && b%s==0){
break;
}
i--;
}
printf("最大公约数为:%d \n",s);
}
4>辗转相除法:
#include <stdio.h>
int main()
{
int a , b , s;
printf("请输入两个整数:");
scanf("%d%d",&a,&b);
printf("a = %d , b = %d \n",a , b);
s = a%b;
while(s!=0){
a = b;
b = s;
s = a%b;
}
printf("最大公约数为:%d",b);
}
以上方法仅供参考,你有什么新的想法可以一起讨论哦!
文章来源: blog.csdn.net,作者:橗釉,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_55804957/article/details/116274653
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END