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

统计每个月兔子的总数

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

递推公式

  1. 1月大的兔子 = 上月将要生的兔子
  2. 2月大的兔子 = 上月1月大的兔子
  3. 3月大的兔子 = 上月2月大的兔子 + 上月3月大的兔子
  4. 将要出生的兔子 = 这个月3月大的兔子+ 这个月2月大的兔子。 新手一枚 勿喷
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int mon = in.nextInt();
        // 1                  0 1 0 0
        // 2                  1 0 1 0
        // 3                  1 1 0 1
        // 4                  2 1 1 1
        // 5                  3 2 1 2
        // 1月大的兔子 = 上月将要生的兔子
        // 2月大的兔子 = 上月1月大的兔子
        // 3月大的兔子 = 上月2月大的兔子 + 上月3月大的兔子
        // 将要出生的兔子 = 这个月3月大的兔子+ 这个月2月大的兔子。
        int[] a1 = new int[31];
        int[] a2 = new int[31];
        int[] a3 = new int[31];
        int[] a4 = new int[31];
        //第一个月大
        a1[0] = 1;
        a2[0] = 0;
        a3[0] = 0;
        a4[0] = 0;
        for(int i=1;i<31;i++){
            a1[i] = a4[i-1];
            a2[i] = a1[i-1];
            a3[i] = a2[i-1]+a3[i-1];
            a4[i] = a3[i]+a2[i];
        }
        System.out.println(a1[mon-1]+a2[mon-1]+a3[mon-1]);
    }
}

全部评论

相关推荐

牛至超人:把哈工大,再加大加粗,看见闪闪发光的哈工大字样,面试官直接流口水
投递字节跳动等公司6个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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