题解 | 汽水瓶
解题思路:
- 纯模拟整个过程
- 对3求余,等于2时,则直接可以结束
- 对3求余,等于1时,无法继续求解,则直接结束
- 整个步骤不难,就是有个绕,有递归的意思
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int lastExNum = 0; while (in.hasNext()) { int input = in.nextInt(); if (input == 0) { return; } int rs = 0; for (;;) { // 兑换数量 int exNum = input / 3; rs += exNum; // 兑换后剩余的数量 int leaveNum = input % 3; if (exNum + leaveNum == 2) { rs += 1; break; } if (exNum + leaveNum == 1) { break; } input = leaveNum + exNum; } System.out.println(rs); } } }