题解 | 汽水瓶
这里要注意一开始都是空瓶子,可以借瓶子,所以喝一瓶仅需要消耗两个空瓶子
import java.util.Scanner; import java.util.ArrayList; import java.util.List; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 List<Integer> list = new ArrayList<>(); while (in.hasNextInt()) { // 注意 while 处理多个 case int a = in.nextInt(); if(a == 0) { list.forEach(e -> { int total = 0; int ceil = e; while(true) { ceil -= 2; if(ceil >= 0) { total ++; } else { break; } } System.out.println(total); }); list.clear(); } else { list.add(a); } } } }