HACKER RANK Algorithms - Implementation - Chocolate Feast - Solution.'C'

                               Algorithms - Implementation - Chocolate Feast - Solution
Problem Statement
Little Bob loves chocolate, and he goes to a store with $N 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 Bob get to eat?
Input Format: 
The first line contains the number of test cases, T
T lines follow, each of which contains three integers, NC, and M.
Output Format: 
Print the total number of chocolates Bob eats.
Constraints: 
1T1000 
2N105 
1CN 
2MN
Sample input
3
10 2 5
12 4 4
6 2 2
Sample Output
6
3
5
Explanation 
In the first case, he can buy 5 chocolates with $10 and exchange the 5 wrappers to get one more chocolate. Thus, the total number of chocolates is 6.
In the second case, he can buy 3 chocolates for $12. However, it takes 4 wrappers to get one more chocolate. He can't avail the offer and hence the total number of chocolates remains 3.
In the third case, he can buy 3 chocolates for $6. Now he can exchange 2 of the 3 wrappers and get 1 additional piece of chocolate. Now he can use his 1 unused wrapper and the 1wrapper of the new piece of chocolate to get one more piece of chocolate. So the total is 5.

Solution:

#include <stdio.h>  
 #include <string.h>  
 #include <math.h>  
 #include <stdlib.h>  
 int main() {  
   int t, n, c, m,temp;  
   scanf("%d", &t);  
   while ( t-- )  
   {  
     scanf("%d%d%d",&n,&c,&m);  
     int answer = 0;   
     /** Write the code to compute the answer here. **/  
     answer=n/c;  
     if(answer>=m)  
     {  
        temp=answer;  
       do{  
         temp-=m;  
         answer++,temp++;  
       }while(temp>=m);  
     }  
     printf("%d\n",answer);  
   }  
   return 0;  
 }  

Thanks for Visiting, Hope this helps you....

Comments

Popular Posts