源程序
int m,n,a,b,r;
cin>>m;
cin>>n;
if(m>n)
{
a = m;
b = n;
}else
{
a = n;
b = m;
}
r = b;
while(r != 0)
{
r = a%b;
a = b;
b = r;
}
完善后的程序
#include<iostream>
using namespace std;
//两数的比较
void compare(int x,int y)
{
int t;
if(x<y)
{
t = y;
y = x;
x = t;
}
}
/辗转相除法
int gcy(int x,int y)
{
compare(x,y);
if(x%y==0)
{
return y;
}
return gcy(y,x%y);
}
void main()
{
int a,b;
cout<<"Please input two number"<<endl;
cin>>a>>b;
cout<<"最大公约数:"<<gcy(a,b)<<endl;
}
运行结果
int m,n,a,b,r;
cin>>m;
cin>>n;
if(m>n)
{
a = m;
b = n;
}else
{
a = n;
b = m;
}
r = b;
while(r != 0)
{
r = a%b;
a = b;
b = r;
}
完善后的程序
#include<iostream>
using namespace std;
//两数的比较
void compare(int x,int y)
{
int t;
if(x<y)
{
t = y;
y = x;
x = t;
}
}
/辗转相除法
int gcy(int x,int y)
{
compare(x,y);
if(x%y==0)
{
return y;
}
return gcy(y,x%y);
}
void main()
{
int a,b;
cout<<"Please input two number"<<endl;
cin>>a>>b;
cout<<"最大公约数:"<<gcy(a,b)<<endl;
}
运行结果