题解 | #最大公约数#
最大公约数
https://www.nowcoder.com/practice/cf4091ca75ca47958182dae85369c82c
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 求出a、b的最大公约数。
     * @param a int整型 
     * @param b int整型 
     * @return int整型
     */
    int gcd(int a, int b) {
        // write code here
        int count = 0; 
        while(a%2==0&&b%2==0){
            a/=2;
            b/=2;
            count++;
        }while(a!=b){
            if(a>b){
                a-=b;
            }else{
                b-=a;
            }
        }
        if(count!=0){
            return (1<<count)*a;
        }else{
            return a;
        }
    }
};
 查看6道真题和解析
查看6道真题和解析 巨人网络公司福利 88人发布
巨人网络公司福利 88人发布