题目描述:

You are given two jugs with capacities x and y litres. There is an infinite amount of water supply available. You need to determine whether it is possible to measure exactly zlitres using these two jugs.

Operations allowed:

  • Fill any of the jugs completely.
  • 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.

解题分析:

这是我本科时算法课上到一道经典题。两个瓶子可能量出的水是两个瓶子容量最大公约数的倍数。所以只要判断z是否可以被x,y的最大公约数整除即可。

具体代码:

 public class Solution {
public static boolean canMeasureWater(int x, int y, int z) {
int n=Math.max(x, y);
int m=Math.min(x, y);
x=n;
y=m;
if(z>x)
return false;
if(z==x||z==y)
return true;
n=fun(x,y);
return z%n==0;
}
//求x,y的最大公约数
public static int fun(int x,int y){
if(x%y==0)
return y;
while(x%y!=0){
int m=Math.max(x-y, y);
int n=Math.min(x-y, y);
x=m;
y=n;
}
return y;
}
}

【leetcode】365. Water and Jug Problem的更多相关文章

  1. 【LeetCode】365. Water and Jug Problem 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学题 相似题目 参考资料 日期 题目地址:http ...

  2. Leetcode 365. Water and Jug Problem

    可以想象有一个无限大的水罐,如果我们有两个杯子x和y,那么原来的问题等价于是否可以通过往里面注入或倒出水从而剩下z. z =? m*x + n*y 如果等式成立,那么z%gcd(x,y) == 0. ...

  3. 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 ...

  4. 365. Water and Jug Problem量杯灌水问题

    [抄题]: 简而言之:只能对 杯子中全部的水/容量-杯子中全部的水进行操作 You are given two jugs with capacities x and y litres. There i ...

  5. 365 Water and Jug Problem 水壶问题

    有两个容量分别为 x升 和 y升 的水壶以及无限多的水.请判断能否通过使用这两个水壶,从而可以得到恰好 z升 的水?如果可以,最后请用以上水壶中的一或两个来盛放取得的 z升 水.你允许:    装满任 ...

  6. 365. Water and Jug Problem

    莫名奇妙找了个奇怪的规律. 每次用大的减小的,然后差值和小的再减,减减减减减减到差值=0为止.(较小的数 和 差值 相等为止,这么说更确切) 然后看能不能整除就行了. 有些特殊情况. 看答案是用GCD ...

  7. 【LeetCode】365.水壶问题

    题目描述 解题思路 思路一:裴蜀定理-数学法 由题意,每次操作只会让桶里的水总量增加x或y,或者减少x或y,即会给水的总量带来x或y的变化量,转为数字描述即为:找到一对整数a,b使得下式成立: ax+ ...

  8. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

  9. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

随机推荐

  1. InfluxDB安装

    参考https://www.influxdata.com/downloads/#influxdb 官网 OS X brew update brew install influxdb Docker Im ...

  2. Codeforces Gym 100338C C - Important Roads tarjan

    C - Important RoadsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contes ...

  3. IOS App动态更新

    框架 JSPatch WaxPatch react-native   方案对比 目前已经有一些方案可以实现动态打补丁,例如WaxPatch,可以用Lua调用OC方法,相对于WaxPatch,JSPat ...

  4. IOS学习之IOS沙盒(sandbox)机制和文件操作

    IOS学习之IOS沙盒(sandbox)机制和文件操作(一) 1.IOS沙盒机制 IOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都 ...

  5. GNU C中x++是原子操作吗?

    http://blog.csdn.net/liuaigui/article/details/4141563

  6. jquery hasClass()、is() 多个

    一..hasClass() hasClass()方法是用来检查被选择的元素是否包含指定的class名,其语法: $(selector).hasClass("className"); ...

  7. CentOS 6.4 安装 thrift-0.9.3

    前言 为了能给.NET的程序提供HBase访问接口需要在Hadoop/Spark集群上安装Thrift Thrift介绍 Thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强 ...

  8. MII、RMII、GMII接口的详细介绍

    转载:http://blog.csdn.net/reille/article/details/6312156 概述: MII (Media Independent Interface(介质无关接口)或 ...

  9. css笔记05:表单

    1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...

  10. Android(java)学习笔记108:通过反射获取私有构造方法并且使用

    反射获取私有构造方法并且使用: 1.获取字节码文件.class对象:          Class c = Class.forName("cn.itcast_01.Person") ...