初级算法——最大公约数与最小公倍数(蓝桥杯)

思路:这里使用的是辗转相除法求最大公约数,而  最小公倍数 = 两数相乘/最大公约数

#include<stdio.h>

int main()
{
int m,n,a,b,c;

printf("input two numbers:");
scanf("%d%d",&a,&b);

m = a;
n = b;

while(b != 0)
{
c = a%b;
a = b;
b = c;
}

printf("the largest common divisor is %d\n",a);
printf("the smallest common multiple is %d",m*n/a);
}

猜你喜欢

转载自www.cnblogs.com/JAYPARK/p/9971662.html
今日推荐