题解 | #汽水瓶#

汽水瓶

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

全部评论

相关推荐

孤寡孤寡的牛牛很热情:为什么我2本9硕投了很多,都是简历或者挂,难道那个恶心人的测评真的得认真做吗
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务