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
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");
}
}
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
100Sample 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");
}
}
相关文章推荐
- hdu 1250 Hat's Fibonacci
- HDU 1250 Hat's Fibonacci
- Hat's Fibonacci hdu 1250
- HDU 1250 Hat's Fibonacci JAVA大数
- hdu-1250-Hat's Fibonacci
- HDU 1250 Hat's Fibonacci
- hdu 1250 Hat's Fibonacci
- hdu-1250-Hat's Fibonacci
- HDU 1250 Hat's Fibonacci
- HDU 1250 Hat's Fibonacci
- HDU 1250-Hat's Fibonacci(大数)
- hdu 1250 Hat's Fibonacci(高精度加法+缩进+斐波那契数)
- hdu 1250 Hat's Fibonacci
- Hat's Fibonacci-hdu-1250
- HDU1250--Hat's Fibonacci hdu(98)
- 【HDU】1250 - Hat's Fibonacci(java - BigDecimal)
- hdu 1250 Hat's Fibonacci
- hdu 1250 Hat's Fibonacci(高精度)
- HDU 1250 Hat's Fibonacci()
- hdu-1250-Hat's Fibonacci(大数问题)