-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathResult.java
More file actions
31 lines (25 loc) · 1.03 KB
/
Result.java
File metadata and controls
31 lines (25 loc) · 1.03 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
package hackrank.algorithm.implement.chocolate;
/**
* @see <a href="https://www.hackerrank.com/challenges/chocolate-feast">Chocolate Feast</a>
*/
public class Result {
/**
* @param moneyAmount initial amount of money to spend on chocolate bars
* @param chocolatePrice price for a single chocolate bar
* @param wrapperExchangeRate number of wrappers needed to exchange for a chocolate bar
* @return total number of chocolate bars eaten
*/
public static int chocolateFeast(int moneyAmount, int chocolatePrice, int wrapperExchangeRate) {
int totalEaten = 0;
int wrappers = 0;
while (moneyAmount >= chocolatePrice) {
int chocolates = moneyAmount / chocolatePrice;
moneyAmount = moneyAmount % chocolatePrice;
wrappers += chocolates;
totalEaten += chocolates;
moneyAmount += wrappers / wrapperExchangeRate * chocolatePrice;
wrappers = wrappers % wrapperExchangeRate;
}
return totalEaten;
}
}