If y is equal to 0, then gcd(x, y) is x; otherwise,gcd(x,y) is gcd(y,x % y), where % is the modulus operator.
代码:
#include <iostream>
using namespace std;
int gcd(int x, int y)
{
if(y == 0)
return x;
else
return gcd(y,x%y);
}
int main()
{
cout << gcd(8,4) << endl;
return 0;
}