22 August 2014

Quiz 18: Find number of chocolates in exchange of wrappers

Problem:
A boy have $N money in his pocket. The price of each chocolate is $C. The store offers a discount: for every M wrappers he gives to the store, he gets one chocolate for free. How many chocolates does boy get to eat?

Input Format: 
The first line contains the number of test cases T(<=1000). 
T lines follow, each of which contains three integers N, C and M

Output Format: 
Print the total number of chocolates Boy eats.

Constraints: 
2N105 

1CN
2MN

Sample Input
3
6 2 2
8 3 4
20 2 2

Sample Output:
5
2
19

Explanations:-
case 1, With 6$, he gets 3 chocolates. So he have 3 wrappers. He exchange 2 wrappers out of 3 to get 4th chocolate and now he have a new wrapper from the 4th chocolate, so total wrappers are 2. He again exchanges them and get 5th chocolate. So answer is 5.

Solution:

chomp($t=<STDIN>);
@output=();
if($t>1000)
{
exit;
}
for($i=0;$i<$t;$i++)
{
chomp($line=<STDIN>);
@arr=split(" ",$line);
if($arr[0]<2 or $arr[0]>100000 or $arr[1]<1 or $arr[1]>$arr[0] or $arr[2]<2 or 

$arr[2]>$arr[0])
{
exit;
}
$c=$arr[0]/$arr[1];
$c= int $c;
$w=$c;
while($w >= $arr[2])
{
$c1=$w/$arr[2];
$c1= int $c1;
$c= $c+$c1;
$w=$w%$arr[2]+$c1;
}
push(@output,$c);
}
foreach(@output)
{
print "$_\n";
}



Tips:
Dont forget to round off number of chocolates, like with$7, if cost of each chocolate is $2, you will get 3 chocolates and not 3.5

No comments:

Post a Comment