题解 | #汽水瓶#
汽水瓶
https://www.nowcoder.com/practice/fe298c55694f4ed39e256170ff2c205f
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int empties = scanner.nextInt(); if (empties == 0) { break; } System.out.println(iteration(empties)); } } //Iteration private static int iteration(int empties) { if (empties == 1) { return 0; } int one = 0, two = 1; int remaining = empties; for (int i = 2; i <= empties; i ++) { if (remaining % 2 == 0) { two = one + 1; } else { two = one; } one = two; remaining --; } return two; } //Recursion private static int recursion(int empties){ if(empties == 1){ return 0; }else if(empties % 2 == 0){ return recursion(empties -1) + 1; }else { return recursion(empties - 1); } } }