LeetCode刷题实战365:水壶问题
Fill any of the jugs with water.
Empty any of the jugs.
Pour water from one jug into another till the other jug is completely full, or the first jug itself is empty.
装满任意一个水壶
清空任意一个水壶
从一个水壶向另外一个水壶倒水,直到装满或者倒空
示例
示例 1:
输入: x = 3, y = 5, z = 4
输出: True
示例 2:
输入: x = 2, y = 6, z = 5
输出: False
解题
class Solution {
public:
bool canMeasureWater(int x, int y, int z) {
if(z > x+y)
return false;
int r;
while(y != 0)//循环结束后,最大公约数是x
{
r = x%y;
x = y;
y = r;
}
return (z==0 || z%x == 0);
}
};
赞 (0)