辗转相除法

#include<stdio.h>

int f(int a,int b)
{
int c;
int temp=0;
if (a<b)
{
temp=a;
a=b;
b=temp;
}
while(a%b!=0)
{
c=b;
b=a%b;
a=c;
if (a<b)
{
temp=a;
a=b;
b=temp;
}
printf("%d %d\n",a,b);
}
return b;
}

int main()
{
int a,b;
scanf("%d",&a);
scanf("%d",&b);
b=f(a,b);
printf("%d",b);
}/*思路:输入a,b两个数字传入函数中把a和b按照大小排序进入辗转相除的循环中,当a%b不等于0时,就一直让a%b,把a%b的值存入b中,原来b的值存入a中。返回最后的b(b就是最大公约数)*/

posted @ 2022-01-13 12:15  20212305杨贯宇  阅读(36)  评论(0编辑  收藏  举报