# 3.C_Demo_最大公约数、最小公倍数

11 篇文章 0 订阅

1、unsigned int GreatestCommonDivisor(unsigned int a,unsigned int b);

2、unsigned int LeastCommonMultiple(unsigned int a,unsigned int b);

#include <stdio.h>

/* 最大公约数 */
/* 返回最大公约数，返回0代表错误 */
unsigned int GreatestCommonDivisor(unsigned int a,unsigned int b){

unsigned int c;

if( a == 0 && b != 0 ){
return b;
}else if( a != 0 && b == 0 ){
return a;
}else if( a == 0 && b == 0 ){
printf("err:both input data are '0'\n");
return 0;
}else{
while(1){
c = a % b;
if(c == 0){
return b;
}else{
a=b;
b=c;
}
}
}
}

/* 最小公倍数 */
/* 返回最小公倍数，返回0代表错误 */
unsigned int LeastCommonMultiple(unsigned int a,unsigned int b){

if( a == 0 || b == 0){
printf("err:input data have '0'\n");
return 0;
}else{
return (a * b / GreatestCommonDivisor(a,b));
}
}
int main(){

printf("GCD:%d\n",GreatestCommonDivisor(20,30));
printf("LCM:%d\n",LeastCommonMultiple(30,20));
return 0;
} 

• 3
点赞
• 0
收藏
觉得还不错? 一键收藏
• 0
评论
01-26 1958
05-06 7080
09-28 1735
10-19 4440
09-05 1225
09-03 1014
09-10 811

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。