问题:有这样一道智力题:“某商店规定:三个空汽水瓶可以换一瓶汽水。小张手上有十个空汽水瓶,她最多可以换多少瓶汽水喝?”答案是5瓶,方法如下:先用9个空瓶子换3瓶汽水,喝掉3瓶满的,喝完以后4个空瓶子,用3个再换一瓶,喝掉这瓶满的,这时候剩2个空瓶子。然后你让老板先借给你一瓶汽水,喝掉这瓶满的,喝完以后用3个空瓶子换一瓶满的还给老板。如果小张手上有n个空汽水瓶,最多可以换多少瓶汽水喝?
题目链接:https://www.nowcoder.com/test/question/fe298c55694f4ed39e256170ff2c205f?pid=1088888&tid=29652107
package com.java;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class BearCount {
public static void main(String[] args) {
try {
BearCount.before();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void before() throws IOException {
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader buffer = new BufferedReader(in);
String ss = "";
List<String> list = new ArrayList<String>();
while ((ss = buffer.readLine()) != null) {
if ("0".equals(ss)) {
break;
}
list.add(ss);
}
System.out.println("店铺优惠活动,三个瓶盖可以兑换一瓶啤酒!!!");
for (String s : list) {
int coverCount = Integer.parseInt(s);
int countTotal=calculation(coverCount);
System.out.println("买"+s+"瓶啤酒可以喝"+countTotal+"瓶");
}
buffer.close();
in.close();
}
public static int calculation(int coverCount) {
int countTotal=coverCount;
if(coverCount > 2){
//剩下不超过3无法兑换的瓶盖
int mol =coverCount%3;
//coverCount/3能兑换几瓶
int replaceCount = coverCount/3;
countTotal=countTotal+replaceCount;
coverCount=replaceCount+mol;
calculation(coverCount);
}
return countTotal;
}
}