從鍵盤接收兩個int型整數m和n,先求出最大公約數,再由"m/最大公約數*n"來求得最小公倍數。程式碼如下:
#include "stdio.h"
int main(int argc,char *argv[]){
int m,n,g;
printf("Input m & n(int m,n>0)...\n");
if(scanf("%d%d",&m,&n)!=2 || m<1 || n<1){//輸入兩個整數,保證大於0
printf("Input error, exit...\n");
return 0;
}
g = (m<n ? m : n)+1;
do{//用窮舉法求GCD
g--;
}while(m%g || n%g);
printf("\nThe GCD is %d\nThe LCM is %d\n",g,m/g*n);
從鍵盤接收兩個int型整數m和n,先求出最大公約數,再由"m/最大公約數*n"來求得最小公倍數。程式碼如下:
#include "stdio.h"
int main(int argc,char *argv[]){
int m,n,g;
printf("Input m & n(int m,n>0)...\n");
if(scanf("%d%d",&m,&n)!=2 || m<1 || n<1){//輸入兩個整數,保證大於0
printf("Input error, exit...\n");
return 0;
}
g = (m<n ? m : n)+1;
do{//用窮舉法求GCD
g--;
}while(m%g || n%g);
printf("\nThe GCD is %d\nThe LCM is %d\n",g,m/g*n);
return 0;
}