用递归求最大公约数

来源:互联网 发布:华为网络挑战赛 编辑:程序博客网 时间:2024/05/18 19:23

Copyright(c)2013,烟台大学计算机学院学生
*All rights reserved.
*文件名称:用递归求最大公约数

*作者:杨飞
*完成日期:2013年 11 月22日
*版本号:v1.0
*对任务及求解方法的描述部分:用递归求最大公约数

我的程序:#include<iostream>
using namespace std;
int gcd(int,int);
int main()
{
int n,m;
cout<<"请输入两个数:"<<endl;
    cin>>n>>m;

cout<<"最大公约数为"<<endl;
    cout<<gcd(n,m)<<endl;
return 0;
}
int gcd(int n,int m)
{
int t,c;
if(n<m)
{
c=n;
n=m;
m=c;
}
if(n%m==0)
{       t=m;
     return t;
}
else
{
t=gcd(m,n%m);
return t;
}
}


运行结果:


心得体会:哎

原创粉丝点击