- 这个是循环打印每个月的,如果要20,就不要循环,直接传个20过去
- 1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
- public class exp2{
- public static void main(String args[]){
- int i=0;
- for(i=1;i<=20;i++)
- System.out.println(f(i));
- }
- public static int f(int x)
- {
- if(x==1 || x==2)
- return 1;
- else
- return f(x-1)+f(x-2);
- }
- }
- 或
- public class exp2{
- public static void main(String args[]){
- int i=0;
- math mymath = new math();
- for(i=1;i<=20;i++)
- System.out.println(mymath.f(i));
- }
- }
- class math
- {
- public int f(int x)
- {
- if(x==1 || x==2)
- return 1;
- else
- return f(x-1)+f(x-2);
- }
- }
复制代码
|