1. 程式人生 > >初級演算法——最大公約數與最小公倍數(藍橋杯)

初級演算法——最大公約數與最小公倍數(藍橋杯)

思路:這裡使用的是輾轉相除法求最大公約數,而  最小公倍數 = 兩數相乘/最大公約數

 

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