【HackerRank】 Chocolate Feast

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   os   strong   io   

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:
2≤N≤105

是有可能有多輪兌換的,比如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 here20         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 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.