Greatest Common Divisor from a set of more than 2 integers

后端 未结 13 983
遇见更好的自我
遇见更好的自我 2020-12-09 04:25

There are several questions on Stack Overflow discussing how to find the Greatest Common Divisor of two values. One good answer shows a neat recursive function

相关标签:
13条回答
  • 2020-12-09 05:24
    int GCD(int a,int b){ 
        return (!b) ? (a) : GCD(b, a%b);
    }
    
    void calc(a){
        int gcd = a[0];
        for(int i = 1 ; i < n;i++){
            if(gcd == 1){
                break;
            }
            gcd = GCD(gcd,a[i]);
        }
    }
    
    0 讨论(0)
提交回复
热议问题