- class Test{
- public static void main(String[] args) {
- System.out.println(getBuyCount(28));
- System.out.println(getBuyCount(50));
- }
- /**
- * number:the number of people that need Coca-Cola
- */
- public static int getBuyCount(int number){
- int buyCount=0;//the number of Coca-Cola that need to buy
- int bottleCap=0;//the number of caps of bottles of Coca-Cola
- int bottleCount=0;//the number of Coca-Cola that actually get
- while(bottleCount<number)
- {
- buyCount++;
- bottleCap++;
- if (bottleCap==3) {
- bottleCap=0;
- bottleCount++;
- bottleCap++;
- }
- bottleCount++;
- }
- return buyCount;
- }
- }
复制代码 |