GCD算法

GCD算法



入门级

[cpp]

  1. int gcd(int a,int b)
  2. {
  3. int r;
  4. while(b>0)
  5. {
  6. r=a%b;
  7. a=b;
  8. b=r;
  9. }
  10. return a;
  11. }


 
位运算

[cpp]

  1. int gcd(int a,int b)
  2. {
  3. while(b^=a^=b^=a%=b);
  4. return a;
  5. }    

递归

[cpp]

  1. int gcd(int a,int b)
  2. {
  3. return (b>0)?gcd(b,a%b):a;
  4. }