#include<stdio.h>
int gcd (int a,int b)
{
int num=0;
int c=0;
int temp;
//两个数一样的情况下,最大公约数是他本身
if (a == b)
{
printf("%d", a);
}
else
{
//保证a是比b大的数
if (a < b)
{
temp = a;
a = b;
b = temp;
}
//当两个数都是偶数的情况下,要先除2直到其中一个数不为偶数
while (a % 2 == 0 && b % 2 == 0)
{
a = a / 2;
b = b / 2;
num++;
}
//核心算法,大数减去小数,一直到减数与差相等为止
while (a != b)
{
c = a - b;
if (c > b)
{
a = c;
b = b;
}
else
{
a = b;
b = c;
}
}
}
if (num != 0)
{
printf("最大公约数为:%d", c * 2 * num);//这里要把两个数都是偶数先约掉的2乘回来
}
else
{
printf("最大公约数为:%d", c);
}
return 0;
}
int main()
{
int a, b;
printf("请输入两个数:");
scanf_s("%d %d", &a, &b);
gcd(a,b);
return 0;
}
标签:加分,temp,int,else,最大公约数,num,printf
From: https://www.cnblogs.com/1314liyang/p/16997683.html