编写主函数根据用户输入的两个整数调用函数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
}
#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;
}