题解 | #汽水瓶#
汽水瓶
https://www.nowcoder.com/practice/fe298c55694f4ed39e256170ff2c205f
老实做题的思路就是递归,每次兑换完如果余2瓶就直接跟老板+1瓶白嫖,然后递归循环.
鸡贼点的做法就直接除以2就行了,两瓶可以卡bug无伤兑换1瓶,但这应该不是出题者的初衷.
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 a = in.nextInt();
if (a != 0) {
int result = getCount(a);
System.out.println(result);
}
}
}
private static int getCount(int n) {
int x;
int y;
if (n == 1 || n == 0) {
return 0;
} else if (n == 2) {
return 1;
} else {
x = n / 3;
y = n % 3;
if(y==2){
return x + 1 + getCount(x);
}else{
return x + getCount(x + y);
}
}
}
}

