题解 | #统计每个月兔子的总数#

统计每个月兔子的总数

https://www.nowcoder.com/practice/1221ec77125d4370833fd3ad5ba72395

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = 1;//兔子总数
        int[] status = new int[3];
        /*兔子的状态:int[3]分别对应
         *int[0]第一个月:不能生
         *int[1]第二个月:不能生
         *int[2]第三个月:能每个月生*/
        status[0] = 1;
        int born=0;
        for(int i = 2;i<=n;i++){
           status[2] += status[1];
           status[1] = status[0];
           born = status[2];//本月所生
           status[0] = born;
           m += born;
           born=0;
        }
        System.out.print(m);

    }
}

全部评论

相关推荐

05-16 11:16
已编辑
东华理工大学 Java
牛客737698141号:盲猜几十人小公司,庙小妖风大,咋不叫她去4️⃣呢😁
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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