G - 很麻煩的題 (hdu1250)
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.
题目翻译:
斐波那契数列的计算方法是将前两个元素相加,前两个元素都是1。
(1)= 1,F (2) = 1, F (3) = 1, (4) = 1, F (n > 4) = F (n - 1) + (2) + F (n) + F (4)
您的任务是接受一个数字作为输入,并打印那个斐波那契数。
输入
每一行都包含一个整数。处理到文件末尾。
输出
对于每种情况,将结果输出到一行中。
样例输入
100
样例输出
4203968145672990846840663646
注意:
测试数据中没有生成超过2005位的斐波那契数列。F(20) = 66526有5位数字。
思路:
二维数组打表,大数加法
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int ans=100000;
int a[7500][410];//二维数组
void init()//打表
{
a[1][1]=1;
a[2][1]=1;
a[3][1]=1;
a[4][1]=1;
int tmp;
for(int i=5; i<7500; i++)
{
tmp=0;
for(int j=1; j<410; j++)
{
tmp+=a[i-1][j]+a[i-2][j]+a[i-3][j]+a[i-4][j];//题意:斐波那契数列
a[i][j]=tmp%ans;
tmp/=ans;
}
}
return ;
}
int main()
{
init();
int n,i;
while(~scanf("%d",&n))
{
i=409;
while(a[n][i]==0)
i--;
printf("%d",a[n][i--]);
for(; i>0; i--)
printf("%05d",a[n][i]);
printf("\n");
}
return 0;
}