365. 水壶问题(gcd)

题目连接:

https://leetcode-cn.com/problems/water-and-jug-problem/

题目大意:

中文题

具体思路:

ax + by = z 求是否有合理的解 ,x ,y 为系数
化简 a * t1 * k + b * t2 * k == z;
然后 k * (a * t1 + b * t2) = z;
也就是说z为 a 和 b 的gcd 的倍数
特判为 0 的时候 以及 使得等式成立的基本条件 x + y >= z

作者:letlifestop-2
链接:https://leetcode-cn.com/problems/water-and-jug-problem/solution/gcd-by-letlifestop-2/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

AC代码:

 1 class Solution {
 2 public:
 3     
 4 
 5     bool canMeasureWater(int x, int y, int z) {
 6                
 7         return z == 0 || ( x + y >= z && z % __gcd( x , y ) == 0);
 8     }
 9     
10 };
原文地址:https://www.cnblogs.com/letlifestop/p/11494188.html