今天的c++练习代码———>"欧几里德算法+函数的递归解决求2个整数的最大公约数"

来源:互联网 发布:yy老虎机源码 编辑:程序博客网 时间:2024/06/05 07:14

2种方法 不解释.

#include <iostream>

using namespace std;

int main(void)

{

int a,b;

cin>>a>>b;

while(b!=0)

{

int temp=b;

b=a%b;

a=temp;

cout<<a<<endl;

return 0;

}

============================================

#include <iostream>

using namespace std;

 

int fun(int a,int b)

{

if(b==0) return a;

else return fun(b,a%b);

}

int main(void)

{

int a,b;

cin>>a>>b;

cout<<fun(a,b)<<endl;

}