力扣第365题 水壶问题

力扣第365题 水壶问题

class Solution {
    public:
    int func(int x, int y)
    {
        if (x < y)
        {
            x = x + y;
            y = x - y;
            x = x - y;
        }
        int temp = 0;
        while (y != 0)
        {
            temp = x % y;
            x = y;
            y = temp;
        }
        return x;
    }

    bool canMeasureWater(int x, int y, int z) {
        if (x + y < z) return false;
        if (x == 0 || y == 0) return z == 0 || x + y == z;
        return z % func(x, y) == 0;
    }

};

posted on 2020-03-24 19:44  woodjay  阅读(157)  评论(0编辑  收藏  举报

导航