求最大公约数的方法1:
#include<iostream>
using namespace std;
//return th greatest common divisor
int gcd(int v1, int v2)
{
while(v2)
{
int temp=v2;
v2 = v1 % v2;
v1 = temp;
}
return v1;
}
int main()
{
int v1,v2;
cin >> v1 >> v2;
int gnum = gcd(v1,v2);
cout << gnum << endl;
}
求最大公约数的方法2:
//recursive version greatest common divisor program
int rgcd(int v1,int v2)
{
if (v2 != 0) //we are done once v2 gets to zero
return rgcd(v2, v1%v2); //recurse ,reducing v2 on each call
return v1;
}