-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1872A.cpp
56 lines (43 loc) · 1.45 KB
/
1872A.cpp
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
#include <bits/stdc++.h>
using namespace std;
int main() {
int caseNum;
// Case count input
cin >> caseNum;
for (int currCase = 0; currCase < caseNum; currCase++) {
float a, b, c;
// Input test case
// a and b repesents mass of water in the vessels
// c represents capacity of cup
cin >> a >> b >> c;
// Base case
if (a == b) { // Mass of water in the both vessel is equal
cout << 0 << endl;
continue;
}
int move = 0; // Vaariable that track number of actions
// Get the maximum mass of water in vessels
float *max_vessel = (a > b) ? &a : &b,
*min_vessel = (a > b) ? &b : &a;
while (a != b) {
float halfSubTot = (*max_vessel - *min_vessel) / 2;
if (halfSubTot <= c) {
*max_vessel -= halfSubTot;
*min_vessel += halfSubTot;
}
else {
*max_vessel -= c;
*min_vessel += c;
}
move++;
}
cout << move << endl;
// Math solution
// cout << ((abs(a - b) - 1) / (2 * c)) - 1 << endl;
// Explain:
// abs(a - b) is to search for difference between vessel a and b,
// then subtract by 1, because we want too keep one of vessel is bigger than the other one
// the rest will to adapt move needed to balance two vessels
}
return 0;
}