最大公约数和最小公倍数

本文提供了一个使用C++编写的程序实例,该程序通过用户输入两个整数,计算并输出这两个数的最大公约数(GCD)和最小公倍数(LCM),并展示了如何使用循环来实现这一功能。

欢迎观看和交流

 

test.h

int get();
int max(int a,int b);
int min(int a,int b);
void print(int a,int b);

 

 

test.cpp

#include<iostream>
using namespace std;
 
#include "test3.h"

int main()
{
 int m,n;
 while(1)
 {
  m=get();
  n=get();
  int m_max=max(m,n);
  int m_min=min(m,n);
  print(m_max,m_min);
 }
 
}


int get()
{
 int i;
 cin>>i;
 return i;
}
int max(int a,int b)
{
 int m=a,n=b;
 if(a==0||b==0)
  return 0;
  else if(a==b)
  return a;
 else
  while(a!=0&&b!=0)
  {
   if(a>b)
    a%=b;
   else
    b%=a;
  }
 if(a==0)return m*n/b;
 else return m*n/a;
}
int min(int a,int b)
{
  if(a==0||b==0)
  return 0;
 else if(a==b)
  return a;
 else
  while(a!=0&&b!=0)
  {
   if(a>b)
    a%=b;
   else
    b%=a;
  }
 if(a==0)return b;
 else return a;
}
void print(int a,int b)
{
 if(a==0||b==0)
  cout<<"不存在"<<endl;
 else
  cout<<"MAX="<<a<<"  "<<"MIN="<<b<<endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值