【leetcode 最大公约数 C++】365. Water and Jug Problem

365. Water and Jug Problem

在这里插入图片描述

class Solution {
    
    
public:
    int GCD(int a, int b) {
    
    
        if(a < b) return GCD(b, a);
        while(b) {
    
    
            int c = a % b;
            a = b;
            b = c;
        }
        return a;
    }
    bool canMeasureWater(int x, int y, int z) {
    
    
        if(x + y < z) return false;
        if(x + y == z) return true;
        int gcd = GCD(x, y);
        if(gcd && z % gcd == 0) return true;
        return false;
    }
};

猜你喜欢

转载自blog.csdn.net/m0_37454852/article/details/113861331