Zhejiang version of "C Programming Language (3rd Edition)" title set Exercises 4-11 rabbit breeding problems (15 points)

Here Insert Picture Description
Solution: Fibonacci number
Month: 123,456,789
log: 112358132134
Why start from the month = 2? Because the Fibonacci number of 0 and 1 item that we have assigned, so starting from the second term.

#include <stdio.h>
int main()
{
    int N, month, x1 = 1, x2 = 1, x3 = 0;
    scanf("%d", &N);
    if (N == 1) // 1对至少需要1个月。
        month = 1;
    else
        for (month = 2; x3 < N; month++)
        { // 斐波那契的迭代。
            x3 = x1 + x2;
            x1 = x2;
            x2 = x3;
        }
    printf("%d", month);
    return 0;
}
Published 165 original articles · won praise 117 · Views 7799

Guess you like

Origin blog.csdn.net/qq_44458489/article/details/105320897