每天编程小题目(oneDay):求每个月兔子总数

题目:有一对兔子,第三个月起,每月生一对小兔子。每对小兔子三个月后也生一对小兔子。
请问每个月有多少兔子?
思路:1 1 2 3 5 8 13 21
发现规律,从第3个月开始:fn(x)=fn(x-1)+fn(x-2);

public class TuZhi {

    //计算某个月的兔子总数
    public static int CountTuZhi(int yue){
        if(yue<3)
        {
            return 1;
        }
        else
        {
            return CountTuZhi(yue-1)+CountTuZhi(yue-2);
        }

    }
    //记录每个月兔子的总数
    public static List OutTuZhiCount(int yue){
        ArrayList list=new ArrayList();
        for (int i = 1; i < yue; i++) {
            list.add(CountTuZhi(i));
        }
        return list;
    }
    public static void main(String[] args) {
        //输入月数
        int yue=20;
        //获取记录
        List outTuZhiCount = OutTuZhiCount(yue);
        //输出结果
        System.out.println(outTuZhiCount);
    }

}

你可能感兴趣的:(日常编程小题目)