码迷,mamicode.com
首页 > 其他好文 > 详细

365. 水壶问题(gcd)

时间:2019-09-09 21:12:31      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:获得   作者   public   http   代码   problem   bsp   授权   stop   

题目连接:

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 };

 

365. 水壶问题(gcd)

标签:获得   作者   public   http   代码   problem   bsp   授权   stop   

原文地址:https://www.cnblogs.com/letlifestop/p/11494188.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!