现在的位置: 首页 > 综合 > 正文

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

2018年05月11日 ⁄ 综合 ⁄ 共 325字 ⁄ 字号 评论关闭

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;

}

抱歉!评论已关闭.