代码如下:
#include <iostream>
using namespace std;
long gcd1(int a,int b);
long gcd2(int a,int b);
int main(){
int i,j;
cout <<"please input two numbers:\n";
cin>>i>>j;
cout<<gcd1(i,j)<<endl;
cout<<gcd2(i,j)<<endl;
}
//递归
long gcd1(int a,int b){
if(a%b==0)
return b;
return gcd1(b,a%b);
}
//非递归
long gcd2(int a,int b){
int temp;
while(b!=0){
temp=a%b;
a=b;
b=temp;
}
return a;
}
运行结果如下: