题解 | #汽水瓶#

汽水瓶

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);
        }
    }
}

全部评论

相关推荐

10-11 17:45
门头沟学院 Java
走吗:别怕 我以前也是这么认为 虽然一面就挂 但是颇有收获!
点赞 评论 收藏
分享
牛客410815733号:这是什么电影查看图片
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务