[CareerCup] 6.3 Water Jug 水罐问题

简介:

6.3 You have a five-quart jug, a three-quart jug, and an unlimited supply of water (but no measuring cups). How would you come up with exactly four quarts of water? Note that the jugs are oddly shaped, such that filling up exactly "half" of the jug would be impossible.

这道题给了我们两个水罐,一个是5夸脱的,一个是3夸脱的,还有无限的水,让我们测量出4夸脱的水,前提是我们不能每次给容器装一半的水,那么这需要来回倒好几次,才能装出4夸脱的水,参见步骤如下:

5夸脱水罐当前水量

3夸脱水罐当前水量

步骤

5

0

装满5夸脱水罐

2

3

用5夸脱水罐装满3夸脱水罐

2

0

倒空3夸脱水罐

0

2

把5夸脱水罐中的水倒入3夸脱水罐

5

2

装满5夸脱水罐

4

3

用5夸脱水罐装满3夸脱水罐

4

 

我们得到了4夸脱的水

本文转自博客园Grandyang的博客,原文链接:水罐问题[CareerCup] 6.3 Water Jug ,如需转载请自行联系原博主。
相关文章
|
4月前
|
人工智能 Java
hdu 1165 Eddy's research II
hdu 1165 Eddy's research II
25 0
|
4月前
|
Java
hdu 1164 Eddy's research I
hdu 1164 Eddy's research I
27 0
|
10月前
Leetcode 365. Water and Jug Problem
一句话理解题意:有容积为x和y升的俩水壶,能不能量出z升的水。 我刚开始看到这题,立马就想了下暴力搜索的可能性,但考虑了下数据大小,立马放弃这个暴力的想法,于是意识到肯定有比较简单的数学方法,其实我自己没想到,后来看还是看了别人的代码,很多博客都直接给出了解法, 但没介绍为什么能这么解。所以我决定解释下我自己的思路。
40 0
LeetCode 365. Water and Jug Problem
有两个容量分别为 x升 和 y升 的水壶以及无限多的水。请判断能否通过使用这两个水壶,从而可以得到恰好 z升 的水?
75 0
LeetCode 365. Water and Jug Problem
|
开发者
牛客第六场-Combination of Physics and Maths
题意:选出一个子矩阵,使得所求的压强最大,压强是指这个子矩阵中每个元素之和 / 这个子矩阵最下面一行的元素之和
54 0
牛客第六场-Combination of Physics and Maths
HDU-1027,Ignatius and the Princess II
HDU-1027,Ignatius and the Princess II
LeetCode之Island Perimeter
LeetCode之Island Perimeter
120 0
LeetCode之Island Perimeter