#include<iostream>
using namespace std;
void main()
{
int num1,num2;//用来保存两个数
int Gcmddivisor;//用来保存*最大公约数
int product;//用来保存两个数的积
cout<<"请输入两个数以计算最大公约数:"<<endl;
cin>>num1>>num2;
product=num1*num2;
if(num1<num2)
{
num1=num1^num2;
num2=num1^num2;
num1=num1^num2;
}
if(num1==num2)
{
cout<<"最大公约数为"<<num2<<endl;
}
else
{
Gcmddivisor=num1-num2;
while(Gcmddivisor!=num2)
{
if(num2<Gcmddivisor)
{
Gcmddivisor=Gcmddivisor^num2;
num2=Gcmddivisor^num2;
Gcmddivisor=Gcmddivisor^num2;
}
num1=num2;
num2=Gcmddivisor;
Gcmddivisor=num1-num2;
}
cout<<"最大公约数为"<<Gcmddivisor<<endl;
cout<<"最小公倍数为"<<product/Gcmddivisor<<endl;
}
}
计算最大公约数方法二
最新推荐文章于 2021-06-19 23:21:51 发布