lpdc99 2022-06-03 14:35 采纳率: 75%
浏览 441
已结题

编写函数gcd_lcm求两个整数的最大公约数和最小公倍数

编写主函数根据用户输入的两个整数调用函数gcd_lcm求解并输出它们的最大公约数和最小公倍数。

输出语句格式:
cout<<"Please input two integers: ";
cout<<"The GCD of them is "<<…<<endl;
cout<<"The LCM of them is "<<…<<endl;

输入输出实例1:
Pleae input two integers: 12 24
The GCD of them is 12
The LCM of them is 24

输入输出实例2:
Pleae input two integers: 12 56
The GCD of them is 4
The LCM of them is 168

参考程序模板:

#include <iostream>
using namespace std;
//only necessary output statments are given here
void gcd_lcm( int num1, int num2, int &gcd, int &lcm ) ;
int main()
{
    //to add other statements you need
    cout<<"Please input two integers: ";

    //to add other statements you need
    cout<<"The GCD of them is "<<gcd<<endl;
    cout<<"The LCM of them is "<<lcm<<endl;
    return 0;
}
void gcd_lcm( int num1, int num2, int &gcd, int &lcm ) 
{
    //to add statements to solve gcd  and lcm

}


  • 写回答

1条回答 默认 最新

  • 吕布辕门 后端领域新星创作者 2022-06-03 14:44
    关注
    
    #include <stdio.h>
    int gcd(int n,int m)
    {
        int i=1,x,y;  /*始终让x最大,y最小*/ 
        if(n>m) {x=n; y=m;}
        else x=m;y=n;
        while (i!=0)
        {
            i=x%y;
            if(i==0) return y;
            else {x=y;y=i;}
        }
    }
    
    int lcm(int GCD,int n,int m)
    {
        int x;
        x=n*m/GCD;
        return x;
    }
    
    int main()
    {
        int n,m,GCD,LCM;
        while (scanf("%d,%d",&n,&m)!=EOF&&m*n!=0)
        {
            GCD=gcd(n,m);
            LCM=lcm(GCD,n,m);
            printf("GCD=%d,LCM=%d\n",GCD,LCM);
        }
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 6月11日
  • 已采纳回答 6月3日
  • 创建了问题 6月3日

悬赏问题

  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。