Fibonacci Number Sequence rabbit is not death 14

Death is not rabbit

Case needs

A pair of rabbits from the first 3 months after birth are born every month one pair of rabbits, bunnies grow up to the third month after the month gave birth to one pair of rabbits, if the rabbit is not dead, asked the second ten months how much is the number of rabbits?

analysis

Here Insert Picture Description
Tip: induction scrutiny, summed up the law, to write code.
Summarized scrutiny:
Month: 0123456
logarithmic: 11235813
summed up the law:
from the first three months, the number of the month is the number of the previous two months and.
Write code to achieve:
1. Define a storage array rabbit 20 month logarithmic.
2. a first position and a second position in the array are assigned to one, from the third position traversing the array.
3. Assignment to its first two data elements for each location the sum of: nums [i] = nums [ i-1] + nums [i-2]

public class ExecDemo {
    public static void main(String[] args) {
        // 1.定义一个数组存储20个月份每个月的兔子对数
        int[] nums = new int[20];//这个20表示的是数组的长度
        // 2.为数组的第一个位置和第二个位置都赋值成1.
        nums[0] = nums[1] = 1 ;
        // 3. 从第三个元素开始为每个位置赋值成它的前两个元素的数据的总和。
        for(int i = 2 ; i < nums.length ; i++ ){
            // 当前元素的元素值赋值成 = 前两个位置的元素和。
            nums[i] = nums[i-1] + nums[i-2];
        }
        // 4.输出第20个月的对数
        System.out.println("第20个月的兔子数据是:"+nums[19]);

    }
}
Published 34 original articles · won praise 16 · views 291

Guess you like

Origin blog.csdn.net/qq_41005604/article/details/105179740