这道题的关键是只有两个瓶子,因此每次只能变化x或者y量的水,因此z = ax + by。而z在为x,y的最大公约数倍数时有解。
class Solution {
public:
bool canMeasureWater(int x, int y, int z) {
if(x + y < z) return false;
if(z == 0) return true;
if(x == 0 || y == 0)
{
if(z == x || z == y) return true;
else return false;
}
return z % gcd(x,y) == 0;
}
};