1544:斐波那契数列

来源:互联网 发布:1分的利息怎么算法 编辑:程序博客网 时间:2024/06/05 20:31

1544:斐波那契数列


Description


1,1,2,3,5,8,13,21,34,55,89……这个数列则称为“斐波纳契数列”

一位不受重力影响的学长提示:"an=an-1+aa-2”


Input


输入数据有多行,第一行是一个整数T,表示测试实例的个数,后面跟着T行,每行一个整数N(N<30)。


Output


对于每个测试实例,输出斐波那契数列的前n项和a1+a2+a3+a4+….+an。


Sample Input


3

3

5

10


Sample Output


4

12

143


#include<iostream>using namespace std;int main(){    int a[1000],T,N;    cin>>T;    while(T--)    {        int count=0;        a[1]=1;        a[2]=1;      for(int j=3;j<=1000;j++)        a[j]=a[j-1]+a[j-2];       cin>>N;      for(int i=1;i<=N;i++)      count=count+a[i];       cout<<count<<endl;       }    }


原创粉丝点击