几种求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最慢

posted @ 2019-07-27 12:19  鸿钧三清  Views(348)  Comments(0Edit  收藏  举报