Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        6年前关闭。
                                                                                            
                
        
我有一个简单的方法是这样的:

public int gcd(int a, int b) {
    while (a!=b) {
        int q = b;
        b = a%b;
        a = q;
    }
    return a;
}


有没有更简单的方法来写一个最大的公分母?特别是while循环中的三行,是否可以简化?

最佳答案

尝试这个:

public static int gcd(int a, int b) {
    int q = b;
    b = a % b;
    a = q;
    return (a != b && a != 0 && b != 0) ? gcd(a, b) : a - b;
}

10-01 22:55