java编程练习01

题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少?

根据数据的规律,运用递归解得

public class helloworld {
    public static void  main(String[] args)
    {
       // System.out.println("hello");
        System.out.println(func(8));
    }
    public static int func(int month)
    {
        if(month<1)
        {
            return 0;
        }
        else if(month==1||month==2)
        {
            return 1;
        }
        else
        {
            return func(month-1)+func(month-2);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/obitosbb/article/details/115179411