Problem Statement
A description of the problem can be found on Hackerrank.
Solution
Calculate count of chocolates: chocolates = money / price
. Count of chocolates
is count of wrappers
, too. Free chocolates are calculated:
- divide
chocolates
with count of chocolates needed fordiscount
- add solution of the division to
chocolates
- new
wrappers
count is:wrappers = wrappers = wrappers - free_choco * discount + free_choco
- if
wrappers
count is greater thandiscount
go to 1.
I created solution in 4 languages:
All solutions are also available on my GitHub.
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
object ChocolateFeast extends App { val lines = io.Source.stdin.getLines().toList.tail lines.foreach { line => val amounts = line.split(" ").map(_.toInt) println(chocolates(amounts(0), amounts(1), amounts(2))) } def chocolates(amount: Int, price: Int, discount: Int): Int = { if(amount <= 0) { return 0 } val count = amount / price return count + freeChoco(count, discount) } def freeChoco(wrappersCount: Int, discount: Int): Int = { if(wrappersCount < discount) { return 0 } val free = wrappersCount / discount return free + freeChoco(wrappersCount - (free * discount) + free, discount) } } |
Java
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 |
import java.util.*; public class ChocolateFeast { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int i = 0; i < t; i++){ System.out.println(solve(in.nextInt(), in.nextInt(), in.nextInt())); } } private static long solve(int N, int C, int M) { int result = 0; int bought = N / C; result += bought; int wrappers = bought; while(wrappers >= M) { int freeChoco = wrappers / M; result += freeChoco; wrappers = wrappers - freeChoco * M + freeChoco; } return result; } } |
JavaScript
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 |
function processData(input) { var lines = input.split('\n'); for(var i = 1; i < lines.length; i++) { var amounts = lines[i].split(' ').map(function(item) { return parseInt(item); }); console.log(chocolates(amounts[0], amounts[1], amounts[2])); } } function chocolates(amount, price, discount) { var result = 0; var bought = parseInt(amount / price); result += bought; var wrappers = bought; while(wrappers >= discount) { var freeChoco = parseInt(wrappers / discount); result += freeChoco; wrappers = wrappers - freeChoco * discount + freeChoco; } return result; } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); }); |
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
test_cases = t = gets.to_i test_cases.times { (n, c, m) = gets.split.map{|i| i.to_i} answer = 0 chocolate = n / c answer += chocolate free_choco = chocolate / m while free_choco > 0 do answer += free_choco wrappers = free_choco * m chocolate -= wrappers chocolate += free_choco free_choco = chocolate / m end puts answer } |