hdu 大菲波数

来源:互联网 发布:淘宝上买官换机的技巧 编辑:程序博客网 时间:2024/04/28 21:24

大菲波数

Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 44   Accepted Submission(s) : 12

Font: Times New Roman | Verdana | Georgia

Font Size:

Problem Description

Fibonacci数列,定义如下:
f(1)=f(2)=1
f(n)=f(n-1)+f(n-2) n>=3。
计算第n项Fibonacci数值。

Input

输入第一行为一个整数N,接下来N行为整数Pi(1<=Pi<=1000)。

Output

输出为N行,每行为对应的f(Pi)。

Sample Input

512345

Sample Output

11235

Source

2007省赛集训队练习赛(2)
此题主要处理大数相加,和两个大数相加思路相同即竖式相加。但它比两个数复杂的地方是,数有几位不知道,应考虑如何计算位数。
#include <iostream>using namespace std;int main(){   int n,i,m,p,h,j,l,t;   cin>>m;   for(i=0;i<m;i++)   {       int s[50010]={1};       cin>>n;       if(n==1) cout<<"1"<<endl;       else{       int a[50000]={0},b[50000]={0};//在此定义保证对于每组数据开始计算时数组a,b里的每一个元素都是0.       b[0]=1;       a[0]=0;       t=1;       for(j=0;j<n-1;j++)       {        h=0;       for(l=0;l<t;l++)       {           s[l]=(a[l]+b[l]+h)%10;           h=(a[l]+b[l]+h)/10;           a[l]=b[l];           b[l]=s[l];       }       while(h)//这里用来记录有几位数,并处理每进一位的情况       {           s[t]=h;           b[t]=s[t];           h=h/10;           t++;       }       }       for(int k=t-1;k>=0;k--)       cout<<s[k];       cout<<endl;       }    }   return 0;}

0 0