您的位置:首页 > 其它

Hat's Fibonacci HDU - 1250

2018-03-10 10:25 441 查看
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. 
InputEach line will contain an integers. Process to end of file. 
OutputFor 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.

   PS:这个题很明显是要用大数加法,但是数组每个位存一个数可能会超时间或超内存,所以自己使用较大的进制。

AC:代码:
#include<stdio.h>
int a[8000][300]={0};
int main()
{
for(int i=1;i<5;i++)
a[i][1]=1;
for(int i=5;i<8000;i++)
{
for(int j=1;j<255;j++)
{
a[i][j]+=a[i-1][j]+a[i-2][j]+a[i-3][j]+a[i-4][j];
a[i][j+1]+=a[i][j]/100000000;
a[i][j]%=100000000;
}
}
int n;
while(scanf("%d",&n)!=EOF)
{
int l=255;
while(a
[l]==0)
l--;
printf("%d",a
[l]);//最前面的数不够8位直接输出
for(--l;l>0;l--)
printf("%08d",a
[l]); //这里的%08d估计大家都不陌生了吧?就是不够8位的时候左侧使用0补够 。
printf("\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: