-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily20.cpp
More file actions
41 lines (38 loc) · 1.01 KB
/
Copy pathdaily20.cpp
File metadata and controls
41 lines (38 loc) · 1.01 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
// Solution 1
class Solution {
public:
int numWaterBottles(int numBottles, int numExchange) {
// keep track of how many full bottles there still are
// starting with x bottles
// empty all remaining bottles
auto full = numBottles;
auto empty = 0;
auto bottles = 0;
while (full + empty >= numExchange) {
bottles += full;
empty += full;
full = empty / numExchange;
empty = empty % numExchange;
// std::cout << "next round (full - empty): " << full << " - " << empty << std::endl;
}
return bottles + full;
}
};
// Solution 2
class Solution {
public:
int numWaterBottles(int numBottles, int numExchange) {
int sum=numBottles;
int x=numBottles;
while(true){
int y=x/numExchange;
int z=x%numExchange;
sum+=y;
x=y+z;
if(x<numExchange){
break;
}
}
return sum;
}
};