用数组来求斐波那契数列问题前20项

输入用例

输出用例
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765

#include<stdio.h>
int main()
{
long int a[46]={1,1}; //定义数组
int n,i=2;
scanf("%d",&n); //输入n
if(n>=1&&n<=46) //对n的大小进行规范
{
if(n==1)printf("%11d",a[0]); //当n等于1时
else if(n==2)printf("%11d%11d",a[0],a[1]); //当n等于2时
else //当大于2时
{
printf("%11d%11d",a[0],a[1]); //首先输出前两个数
while(i<n) //通过while循环,以此输出第三个、第四个数,以此类推
{
a[i]=a[i-1]+a[i-2]; //从第二个数开始,每一个数都等于前两个数之和
printf("%11d");
i++;
if(i%5==0)printf("\n"); //当一行中输出五个数时,换行
}
}if(n%5!=0)printf("\n"); //若是最后一行输出不满足五个数,换行
}else printf("Invalid."); //当n大于题目给定范围时
}

建议参考:https://blog.csdn.net/weixin_43919932/article/details/120147474

int tribonacci__(int n) 
{
    int t0 = 1, t1 = 0, t2 = 0, tt = 0;
    while(n > 0)
    {
        tt = t0 + t1 + t2;
        t0 = t1;
        t1 = t2;
        t2 = tt;
        n -= 1;
    } 
    return tt;
}