Chocolate Feast[HackerRank Solution]

1 minute

read

Chocolate Feast[HackerRank Solution]

Problem:

Little Bobby loves chocolate, and he frequently goes to his favorite  store, Penny Auntie, with  dollars to buy chocolates. Each chocolate has a flat cost of  dollars, and the store has a promotion where they allow you to trade in  chocolate wrappers in exchange for  free piece of chocolate.

For example, if  and Bobby has  dollars that he uses to buy  chocolates at  dollar apiece, he can trade in the  wrappers to buy  more chocolates. Now he has  more wrappers that he can trade in for  more chocolate. Because he only has  wrapper left at this point and , he was only able to eat a total of  pieces of chocolate.

Given n ,m ,c and t for  trips to the store, can you determine how many chocolates Bobby eats during each trip?

Input Format

The first line contains an integer, , denoting the number of trips Bobby makes to the store.
Each line  of the  subsequent lines contains three space-separated integers describing the respective , and values for one of Bobby’s trips to the store.

Constraints

Output Format

For each trip to Penny Auntie, print the total number of chocolates Bobby eats on a new line.

Sample Input

3
10 2 5
12 4 4
6 2 2

Sample Output

6
3
5

Code:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

void choco(int n,int m,int c)
{
 int nos = n/m ;
 int ans = nos;
 while(nos >= c)
 {
 ans++;
 nos = nos - c;
 nos++;
 }
 
 cout<< ans<<endl;
}

int main(){
 int t;
 cin >> t;
 for(int a0 = 0; a0 < t; a0++){
 int n;
 int c;
 int m;
 cin >> n >> c >> m;
 choco(n,c,m);
 }
 return 0;
}

 

Easy Algorithm Problem!

2 responses to “Chocolate Feast[HackerRank Solution]”

  1. A lot of thanks for all of your work on this blog. My mom take interest in working on research and it’s really obvious why. All of us learn all of the lively way you provide useful guidelines through the web blog and therefore increase response from some others on that article so my daughter is without question discovering a lot. Have fun with the rest of the new year. You’re doing a powerful job.

    Liked by 1 person

Leave a comment