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

时间:2022-08-04 00:29:57

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

 

#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);
}