几种求gcd的方法
1 int gcd1(int a, int b){ 2 return b > 0 ? gcd1(b, a % b) : a; 3 } 4 5 int gcd2(int a, int b){ 6 while(b ^= a ^= b ^= a %= b); 7 return a; 8 } 9 10 int gcd3(int a, int b){ 11 int r; 12 while(b > 0){ 13 r = a % b; 14 a = b; 15 b = r; 16 } 17 return a; 18 }
简单测试发现3最快1最慢