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

import java.util.*;

// 注意类名必须为 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();
            /**
             * 斐波那契数列
             * 1月 1 
               2月 1
               3月 2
               4月 3
               5月 5
               6月 8
               结论:N月=(N-1)月+(N-2)月
             */
            System.out.println(compute(a));
        }
    }
    public static int compute(int month){
        if(month==1 || month ==2){
            return 1;
        } else{
            return compute(month-1)+compute(month-2);
        }
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-08 14:08
点赞 评论 收藏
分享
每晚夜里独自颤抖:你cet6就cet6,cet4就cet4,你写个cet证书等是什么意思。专业技能快赶上项目行数,你做的这2个项目哪里能提现你有这么多技能呢
点赞 评论 收藏
分享
06-10 23:36
已编辑
首都经济贸易大学 C++
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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