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

统计每个月兔子的总数

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

JAVA

import java.util.*;
//佛波那契数列,从第三个数开始,每个数等于前面两个数之和。
// 1 1 2 3 5 8 13 21 34
//用递归。
public class Main{
    public static void  main(String[] args){
        Scanner sc = new Scanner(System.in);
        //多组数据输入就要用while(sc.hasNext())
        while(sc.hasNext()){
        int month = sc.nextInt();
        System.out.println(getAllCount(month));
        }

    }

    public static int getAllCount(int month){
        if(month<=0){
            return -1;
        }
        //当月份小于3
        if(month<3){
            return 1;
        }else{
            //从三月开始为上个月和上上个月之和
            return getAllCount(month -1) + getAllCount(month -2);
        }

    }
}
全部评论

相关推荐

点赞 评论 收藏
分享
零零幺零零幺:至少再做一个项目,然后猛投小厂,不然有点难
点赞 评论 收藏
分享
求问!考研下岸,打算参加春招,我这个bg能进啥厂,或者需要搞点深度项目再投吗
Java抽象带篮子_...:直接海投,可以看看我的考研失利速成冲春招贴,里面详细写了简历怎么写,学哪些项目可以速成
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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