Leetcode 365
来源:互联网 发布:会员数据分析公司 编辑:程序博客网 时间:2024/04/30 03:12
题意
jug problem,给2个罐子,容积为x和y,给定整数z,问能否用x和y凑出z的水。
可做的:
- 将任一罐子装满水。
- 将任一罐子的水倒掉。
- 将一个罐子的水倒到另一个罐子直到该罐子为空或者另一罐子装满。
思路
如果求最短步数可以通过bfs来做,这道题只需要判断能否有解。
其中步骤3是水在x和y之间流通并不会造成损失,而步骤1和2可以列出如下方程:
即我们要做的就是判断是否有解。
根据裴蜀定理,我们只需要判断z是否是x和y的最大公约数g的整数倍即可。
并且还需要满足
代码
class Solution {public: bool canMeasureWater(int x, int y, int z) { if (x == 0 || y == 0) return z == x || z == y; int g = __gcd(x, y); return !(z % g) && x + y >= z; }};
0 0
- Leetcode 365
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode:
- 算法粗略见解
- Linux make笔记
- 习题30 else 和 if. 附加练习3
- 一种基于中值滤波的轨迹纠偏方法和几点思考
- JAVA多线程实现的四种方式
- Leetcode 365
- Majority Element
- BufferInputStream与BufferOutputStream
- HDU-1274-展开字符串(dfs分段字符串处理)
- 对模拟form表单的疑问
- Linux动态库搜索路径
- css的样式
- 输入子系统 OK6410按键驱动
- flask运行环境搭建(nginx+gunicorn)