求两个数的最大公约数和最小公倍数

1 如何求最大公约数
方法:辗转相除法
方法介绍: 始终用较大数除以较小数,然后用余数代替较大数。整除时的除数就是最大公约数。举例:
222 407求最大公约数:
222 407(407除以222余数185)
222  185(222除以185余数37)
37   185(185除以37余数0)
所以最大公约数为37

C语言实现
#include<stdlib.h>
#include<stdio.h>
//求两个数的最大公约数
int Maxcd(int a,int b)
{
 int c,tmp;
 if(a<b)
 {
  tmp=a;
  a=b;
  b=tmp;
 }
 c=a%b;
 if(c==0)
  return b;
 else
  tmp=b%c;
     while(tmp!=0)
  {
   b=c;
   c=tmp;
   tmp=b%c;
  }
  return c;
}

int main()
{
 int a,b;
 printf("please input two numbers:/n");
 scanf("%d%d",&a,&b);
 printf("the Max common divisor is:%d/n",Maxcd(a,b));
 return 0;
}

2 求两个数的最小公倍数
方法:用两个数的乘积除以它们的最大公约数
C语言实现
#include<stdlib.h>
#include<stdio.h>

int Maxcd(int a,int b)
{
 int c,tmp;
 if(a<b)
 {
  tmp=a;
  a=b;
  b=tmp;
 }
 c=a%b;
 if(c==0)
  return b;
 else
  tmp=b%c;
     while(tmp!=0)
  {
   b=c;
   c=tmp;
   tmp=b%c;
  }
  return c;
}

int main()
{
 int a,b;
 printf("please input two numbers:/n");
 scanf("%d%d",&a,&b);
 printf("the Max common divisor is:%d/n",Maxcd(a,b));
 //求两个数的最小公倍数
 printf("the Min common divisor is:%d/n",(a*b)/Maxcd(a,b));
 return 0;
}

   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值