该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
昨天我也看到了这题,答案是15,并且还剩3个瓶盖1个空瓶。我特地编了个java帮忙解,可以直接复制整个代码运行:
import java.util.*;
public class Main
{
static int input = 12; // 初始金钱
static int output = 0;
static final int PRICE = 2; //价格
static final int exForEMPTY = 2;
static final int exForCAPS = 4;
static int curCaps = 0;
static int curEmpty = 0;
static int bottleFromCaps = 0;
static int bottleFromEmpty = 0;
public static void main(String[] args)
{
output += ( input / PRICE );
curCaps = output;
curEmpty = output;
do {
bottleFromCaps = exCaps(curCaps);
bottleFromEmpty = exEmpty(curEmpty);
output += ( bottleFromCaps + bottleFromEmpty );
} while( (bottleFromCaps != 0 ) || (bottleFromEmpty != 0 ) );
System.out.println("总共瓶数: " + output);
System.out.println("剩余空瓶: " + curEmpty);
System.out.println("剩余瓶盖: " + curCaps);
}
public static int exCaps(int num) {
if ( num < exForCAPS ) return 0;
int bottles = num / exForCAPS;
curCaps = ( num % exForCAPS ) + bottles;
curEmpty += bottles;
return bottles;
}
public static int exEmpty(int num) {
if ( num < exForEMPTY ) return 0;
int bottles = num / exForEMPTY;
curEmpty = ( num % exForEMPTY ) + bottles;
curCaps += bottles;
return bottles;
}
}