-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL365.java
More file actions
58 lines (47 loc) · 1.26 KB
/
L365.java
File metadata and controls
58 lines (47 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.liang.leetcode;
/**
* @ClassName L365
* @description water-and-jug-problem
* @Author LiaNg
* @Date 2018-12-21
*/
public class L365 {
public static void main(String[] args) {
int x = 3;
int y = 5;
int z = 0;
L365 l = new L365();
System.out.println(l.canMeasureWater(x, y, z));
}
/**
* 有两个容量分别为 x升 和 y升 的水壶以及无限多的水。请判断能否通过使用这两个水壶,从而可以得到恰好 z升 的水?
* 如果可以,最后请用以上水壶中的一或两个来盛放取得的 z升 水。
* 你允许:
* 装满任意一个水壶
* 清空任意一个水壶
* 从一个水壶向另外一个水壶倒水,直到装满或者倒空
*/
public boolean canMeasureWater(int x, int y, int z) {
if (z == 0) {
return false;
}
if (x + y < z) {
return false;
}
if (x < y) {
x = x + y;
y = x - y;
x = x - y;
}
if (y == 0) {
return x == z;
}
int gcd = 0;
while (x % y != 0) {
gcd = x % y;
x = y;
y = gcd;
}
return z % y == 0;
}
}