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!

Leave a comment