365 Water and Jug Problem 水壶问题
有两个容量分别为 x升 和 y升 的水壶以及无限多的水。请判断能否通过使用这两个水壶,从而可以得到恰好 z升 的水?
如果可以,最后请用以上水壶中的一或两个来盛放取得的 z升 水。
你允许:
装满任意一个水壶
清空任意一个水壶
从一个水壶向另外一个水壶倒水,直到装满或者倒空
示例1: (From the famous "Die Hard" example)
输入: x = 3, y = 5, z = 4
输出: True
示例2:
输入: x = 2, y = 6, z = 5
输出: False
详见:https://leetcode.com/problems/water-and-jug-problem/description/
C++:
class Solution {
public:
bool canMeasureWater(int x, int y, int z) {
return z==0||(x+y>=z&&z%gcd(x,y)==0);
}
int gcd(int x,int y)
{
return y==0?x:gcd(y,x%y);
}
};
参考:https://www.cnblogs.com/grandyang/p/5628836.html
365 Water and Jug Problem 水壶问题的更多相关文章
- 365. Water and Jug Problem (GCD or BFS) TBC
https://leetcode.com/problems/water-and-jug-problem/description/ -- 365 There are two methods to sol ...
- 【leetcode】365. Water and Jug Problem
题目描述: You are given two jugs with capacities x and y litres. There is an infinite amount of water su ...
- 365. Water and Jug Problem量杯灌水问题
[抄题]: 简而言之:只能对 杯子中全部的水/容量-杯子中全部的水进行操作 You are given two jugs with capacities x and y litres. There i ...
- 【LeetCode】365. Water and Jug Problem 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学题 相似题目 参考资料 日期 题目地址:http ...
- Leetcode 365. Water and Jug Problem
可以想象有一个无限大的水罐,如果我们有两个杯子x和y,那么原来的问题等价于是否可以通过往里面注入或倒出水从而剩下z. z =? m*x + n*y 如果等式成立,那么z%gcd(x,y) == 0. ...
- 365. Water and Jug Problem
莫名奇妙找了个奇怪的规律. 每次用大的减小的,然后差值和小的再减,减减减减减减到差值=0为止.(较小的数 和 差值 相等为止,这么说更确切) 然后看能不能整除就行了. 有些特殊情况. 看答案是用GCD ...
- [Swift]LeetCode365. 水壶问题 | Water and Jug Problem
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...
- [LeetCode] Water and Jug Problem 水罐问题
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...
- Leetcode: Water and Jug Problem && Summary: GCD求法(辗转相除法 or Euclidean algorithm)
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...
随机推荐
- Linux下汇编语言学习笔记55 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- 1sting 大数 递推
You will be given a string which only contains ‘1’; You can merge two adjacent ‘1’ to be ‘2’, or lea ...
- abs 暴力
Given a number x, ask positive integer y≥2y≥2, that satisfy the following conditions: 1. The absolut ...
- 笔记:Javac编译器
Javac编译器是把 *.java 文件转换为 *.class 文件,是一个前端编译器:对应着有一种把字节码转变为机器码的编译器,称为JIT编译器(Just In Time Compiler),比如 ...
- 使用Post方法模拟登陆爬取网页(转)
使用Post方法模拟登陆爬取网页 最近弄爬虫,遇到的一个问题就是如何使用post方法模拟登陆爬取网页.下面是极简版的代码: import java.io.BufferedReader; impor ...
- 【CV论文阅读】Deep Linear Discriminative Analysis, ICLR, 2016
DeepLDA 并不是把LDA模型整合到了Deep Network,而是利用LDA来指导模型的训练.从实验结果来看,使用DeepLDA模型最后投影的特征也是很discriminative 的,但是很遗 ...
- scala 入门Eclipse环境搭建及第一个入门经典程序HelloWorld
scala 入门Eclipse环境搭建及第一个入门经典程序HelloWorld 学习了: http://blog.csdn.net/wangmuming/article/details/3407911 ...
- IOS-Storyboard控制器切换之Modal(1)
Modal模式是指模态切换.新开的界面会挡住之前的界面,使之不能获取焦点. 创建一个singleView模板的程序,打开storyboard文件.拖动2个UIViewController到界面中.按住 ...
- 修改windows下mysql的max_allowed_packet的值
执行sql报错:Error updating database. Cause: com.mysql.jdbc.PacketTooBigException: Packet for query is to ...
- scikit-learn:3.3. Model evaluation: quantifying the quality of predictions
參考:http://scikit-learn.org/stable/modules/model_evaluation.html#scoring-parameter 三种方法评估模型的预測质量: Est ...