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

来源:互联网 发布:我是传奇结局知乎 编辑:程序博客网 时间:2024/06/09 19:56

题目:有一对兔子,第三个月起,每月生一对小兔子。每对小兔子三个月后也生一对小兔子。
请问每个月有多少兔子?
思路: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<Integer> list=new ArrayList<Integer>();        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);    }}
0 0