歐幾里德算法求最大公約數
在求兩個數的最大公約數方法中,
輾轉相除法是比較快的一種方法。
也就是著名的歐幾里德方法。
View Code
int Gcd(int a, int b){ return b==0?a:gcd(b, a%b); }
View Code
#include "iostream"
#include "cstdio"
#include "cstring"
#include "string"
#include "algorithm"
using namespace std;
int Gcd(int a, int b)//毆幾里得算法,求最大公約數
{
if(b==0) return a;
else return Gcd(b, a%b);
}
int main()
{
int x, y;
while(cin>>x>>y)
{
cout<<Gcd(x, y)<<endl;
}
}
posted on 2012-04-05 16:48 More study needed. 閱讀(204) 評論(0) 收藏 舉報

浙公網安備 33010602011771號