题解 | #汽水瓶#
汽水瓶
https://www.nowcoder.com/practice/fe298c55694f4ed39e256170ff2c205f
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
List<Integer> list = new ArrayList<>();
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) {
int inputVal = Integer.parseInt(in.nextLine());
if (inputVal == 0) {
for (Integer e : list) {
System.out.println(e);
}
} else {
list.add(calculate(inputVal));
}
}
}
private static int calculate(int bottleCount) {
if (bottleCount <= 1) {
return 0;
} else if (bottleCount <= 3) {
return 1;
}
int drinkCount = bottleCount / 3;
int mod = bottleCount % 3;
int total = drinkCount + mod;
return calcaulate(total) + drinkCount;
}
}
查看14道真题和解析