问题及代码:
/*
*copyright (t) 2016,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:main.cpp
*作者:郝昱猛
*完成日期:2016年3月31日
*版本号:v1.0
*问题描述:用递归函数求两个数的最大公约数。
*程序输出:。
*/
#include<iostream>
using namespace std;
int gcd(int x,int y);
int main()
{
int a,b,g;
cin>>a>>b;
g=gcd(a,b);
cout<<"最大公约数是:"<<g;
return 0;
}
int gcd(int x,int y)
{
int i=0;
while(y!=0)
{ i=x%y;
x=y;
y=i;
}
return x;
}
运行结果: