Java 想到的一种写法:
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int n = in.nextInt(); // 空瓶子数量
if (n == 0) { // 如果是0 跳出循环
break;
}
int result = 0;
while (n >= 2) {
if (n == 2) {
result += 1; // 如果只剩余两个空瓶子,按题意可以借一个空瓶子,所以最后结果+1
break;
}
int full = n / 3; // 换到的饮料个数
int empty = n % 3; // 换完饮料后,未喝前剩余空瓶子个数
result += full; // 换到的饮料可以加到最后的总数上
n = full + empty; // 换完的饮料喝完加上之前剩下来的空瓶子,就是手里所有的空瓶子
}
System.out.println(result);
}
}
}
京公网安备 11010502036488号