编写计算斐波那契数列的第n项(数组)

img

img


大神帮我看看我写的有啥问题,为啥输出要么是0,要么是个随机很大的数

你的n不能超过20,超过就内存越界了,你说的随机数

用这个方法


    int count=70;
    int N1=1,N2=1;
    int res=0;
    for(int i=3;i<=count;++i)
    {
        res = N1+N2;
        N1=N2;
        N2=res;
    }
         
         
    printf("%d\n",res);