【HackerRank】 Chocolate Feast

Little Bob loves chocolates, and goes to the store with $N money in his pocket. The price of each chocolate is $C. The store offers a discount: for every M wrappers he gives the store, he'll get one chocolate for free. How many chocolates does Bob 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 Bob eats.

Constraints:
2N105


是有可能有多轮兑换的,比如N=10,C=3,M=2的时候,第一轮买到5个巧克力,用其中四个换回两块,此时手上一共有3个包装纸,第二轮换到1个巧克力,此时手上有两个包装纸,又可以换一块巧克力,一共兑换了3轮。

代码如下:

 1 import java.io.*;
 2 import java.util.*;
 3 import java.text.*;
 4 import java.math.*;
 5 import java.util.regex.*;
 6 
 7 public class Solution {
 8 
 9     public static void main(String[] args) {
10         Scanner in = new Scanner(System.in);
11         int t = in.nextInt();
12         for(int i = 0; i < t; i++){
13             System.out.println(Solve(in.nextInt(), in.nextInt(), in.nextInt()));
14         }
15     }
16     
17     private static long Solve(int n, int a, int b){
18         
19          //Write code to solve each of the test over here
20         int origin = n/a;
21         int total = origin;
22         while(origin/b>0){
23             int free = origin/b;
24             origin = free + origin%b;
25             total += free;
26         }
27         return total;
28     }
29     
30     
31 }

 

转载于:https://www.cnblogs.com/sunshineatnoon/p/3875888.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值