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

编写函数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日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改