The primary rabbit series production subproblem

Rabbit birth problems

Description of the problem
with a pair of rabbits from the first 3 months after birth are born every month one pair of rabbits, bunnies grow up to 3 months after the birth of each month and one pair of rabbits, rabbits do not assume that all of the dead and asked the total number of rabbits per month within 30 months how much?
Analysis
Total rabbit months
. 1. 1
2. 1
. 3 2
. 4. 3
. 5. 5
. 6. 8
. 7 13 is
the Fibonacci series

algorithm design

Iteration of the loop, that is a constant variable to replace the old value with the new value, new variables introduced by the variable value and then handed the old value of
the process, this iteration related to the following factors: initial value, iterative formula, the number of iterations

Iterative formula
fib1 = fib2 = 1 (n = 1, 2) the initial value of
fib (n) = fib (n -1) + fib (n-2) (n> = 3) iterative formula

/* !< use c */
#include <stdio.h>
#include <stdlib.h>

int main()
{
    long fib1 = 1, fib2 = 1, fib;
    int i;
    printf("%12ld%12ld", fib1, fib2);       //输出第一月和第二个月的兔子数
    for (i = 3; i <= 30; i++)
    {
        fib = fib1 + fib2;                  //迭代求出当前月份的兔子数
        printf("%12d", fib);                //输出当前月份兔子数
        if (i % 4 == 0)
            printf("\n");                   //每行输出4个
        fib1 = fib2;                        //为下一次迭代作准备, 求出新的fib2
        fib2 = fib;                         //求出新的fib1
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/xuzhaoping/p/11484492.html