Problem Description
A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1.
F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4) Your task is to take a number as input, and print that Fibonacci number. |
Input
Each line will contain an integers. Process to end of file.
|
Output
For each case, output the result in a line.
|
Sample Input
100 |
Sample Output
4203968145672990846840663646 Note: No generated Fibonacci number in excess of 2005 digits will be in the test data, ie. F(20) = 66526 has 5 digits. |
Author
戴帽子的
|
Recommend
Ignatius.L
|
题目给出公式
F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4),然后给出n,要求出F[n],数据比较大,要用到大数运算
代码:
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
int f[4][2101];
int ans[2101];
int main()
{
int N;
while(cin>>N)
{
if(N<=4)
{
cout<<"1"<<endl;
continue;
}
memset(f,0,sizeof (f));
memset(ans,0,sizeof (ans));
f[0][0]=f[1][0]=f[2][0]=f[3][0]=1;
int len=1;
for(int i=4;i<N;i++)
{
int carry=0;
for(int j=0;j<len;j++)
{
int temp=carry+f[0][j]+f[1][j]+f[2][j]+f[3][j];
ans[j]=temp%10;
carry=temp/10;
}
while(carry!=0)
{
ans[len++]=carry%10;
carry/=10;
}
for(int j=0;j<len;j++)
{
f[i%4][j]=ans[j];
}
}
for(int i=len-1;i>=0;i--)
cout<<ans[i];
cout<<endl;
}
}
学 java啊 学 java 啊!!!!!!!!!!!!!!!!!!!