题解 | #汽水瓶#递归
汽水瓶
https://www.nowcoder.com/practice/fe298c55694f4ed39e256170ff2c205f
用递归就行,纯粹的数学题
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();
// int b = in.nextInt();
// System.out.println(a + b);
// }
while(in.hasNextInt()){
int n=in.nextInt();
if(n==0){
continue;
}
int result=howmany(n);
System.out.println(result);
}
}
public static int howmany(int n){
int count=0;
if(n==0||n==1){
return 0;
}
if(n==2){
return 1;
}
if(n%3==0&&n>=3){
count=n/3;
return count+howmany(count);
}else {
int last=n%3;
count=n/3;
return count+howmany(count+last);
}
}
}
