简体   繁体   English

生成斐波那契数是 C

[英]Generating fibonacci number is C

I was practicing array in C. I have found a code mentioned bellow.我在 C 中练习数组。我找到了下面提到的代码。


int main(void)
{
    int  numbers[10] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    int  i, j;

    **for (j = 0;  j < 10;  ++j)
        for (i = 0;  i < j;  ++i)
            numbers[j]  = numbers[j] +numbers[i];**

    for (j = 0;  j < 10;  ++j)
        printf("%i ", numbers[j]);

    printf("\n");

    return 0;
}

I know that it's a silly question.我知道这是一个愚蠢的问题。 If anyone help me to understand the Bold marked for loop, I would be very grateful.如果有人帮助我理解标记为粗体的 for 循环,我将不胜感激。 Thanks in advance.提前致谢。

for (j = 0;  j < 10;  ++j)
    for (i = 0;  i < j;  ++i)
        numbers[j]  = numbers[j] +numbers[i];

The above loop will target particular position of the array and will add all the numbers one by one from 0th position to selected position.上面的循环将针对数组的特定位置,并将从第 0 个位置到选定位置的所有数字一一相加。

Eventually you are not generating the Fibonacci sequence.最终您不会生成斐波那契数列。


j =0
       1, 0, 0, 0, 0, 0, 0, 0, 0, 0

j = 1
       1, 1, 0, 0, 0, 0, 0, 0, 0, 0

j = 2
       1, 1, 2, 0, 0, 0, 0, 0, 0, 0

j = 3
       1, 1, 2, 4, 0, 0, 0, 0, 0, 0

j = 4
       1, 1, 2, 4, 8, 0, 0, 0, 0, 0

...so on

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM