题解 | #汽水瓶#

汽水瓶

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 n = in.nextInt();
            if(n <= 0){
                break;
            }else{
                System.out.println(getBotten(n));
            }
            
        }
    }
    public static int getBotten(int num){
        int all = 0; // 总共可换瓶子数量
        int i = num/3; // 除数,可以换的个数
        int j = num%3; //余数,换完剩下来的
        int last = i+j;
        if(last > 3){
            all = getBotten(last) + i;
            return all;
        }else if(last == 2 || last == 3){
            return all+i+1;
        }else{
            return all+i;
        }
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务