import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public int getCount(int month) { if (month <= 2) { return 1; } return getCount(month - 1) + getCount(month - 2); } public static void main(String[] args) throws IOException { Main main = new Main(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s ; while((s=br.readLine())!=null && !s.equals("0")) { System.out.println(main.getCount(Integer.valueOf(s))); } } }
每个月的兔子数量:
1月 1
2月 1
3月 2
5月 5
6月 8
7月 13
8月 21
9月 34
10月 55
11月 89
12 144
通过数据我们发现f(n) = f(n-1)+f(n-2),此时条件是3月开始,1,2月小兔子还没下崽,刚好满足斐波那契数列公式。