201111621301
int gcd(int m,int n)
{
if(m<n)
{
int tem;
tem=m;
m=n;
n=tem;
}
for(int t;t!=0;)
{
t=m%n;
m=n;
t=n;
}
return m;
}
完善后的程序
#include<iostream.h>
int gcd(int m,int n)
{
int t;
while(n!=0)
{
t=m%n;
m=n;
n=t;
}
return m;
}
int main()
{
int a,b;
cout<<"input two number:";
cin>>a>>b;
cout<<"两个整数的最大公约数是:"<<gcd(a,b)<<endl;
return 0;
}
编程结果:
《算法概论》思维导图:XMind版本