废话不说,直接上代码
如有错误欢迎指正
#include <cstdlib>
using namespace std;
void exchange( int& m, int& n )
{
int t = m;
m = n;
n = t;
return;
}
bool is_positive( int m )
{
return m > 0 ;
}
int gcd( int m, int n )
{
if( !is_positive(m) || !is_positive(n) ) return -1; // for fail
int r = m % n;
if( 0 == r ) return n;
else return gcd( n, r );
}
int gcd_1( int m, int n )
{
if( !is_positive(m) || !is_positive(n) ) return -1; // for fail
if( m < n )
{
exchange( m, n );
}
int r = 0;
do{
r = m % n;
m = n;
n = r;
}while( r != 0 );
return m; // it is trick, current m is the last n, n is the current r;
}