Hangzhou Electric ACM 1250 Hat's Fibonacci (need to see)

Source: Internet
Author: User
Tags numeric value
Hat ' s Fibonacci problem Description A Fibonacci sequence is calculated by adding the previous and the sequence, with the first and both members being B Oth 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 would contain an integers. Process to end of file.
Output for each case, the output of the result in a line.Sample Input
Sample Output
4203968145672990846840663646 Note:no generated Fibonacci number in excess of 2005 digits would be in the test data, ie. F () = 66526 has 5 digits.

Note: Unlike other large numbers, when calculating the result of adding large numbers, each bit of the array is stored in 10000, not 10, because 10 memory is super

AC Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define M 7250
int fib[m][600];
void Fibonacci ()
{
memset (fib,0,sizeof (FIB));
int i,j;
fib[0][0]=0;
Fib[1][0]=1;
Fib[2][0]=1;
Fib[3][0]=1;
Fib[4][0]=1;
for (i=5;i<7200;i++)
{
for (j=0;j<600;j++)
{
The fib[i][j]=fib[i][j]+fib[i-1][j]+fib[i-2][j]+fib[i-3][j]+fib[i-4][j];//calculates the result by adding the number on the bit to the number above the four digits.
fib[i][j+1]=fib[i][j+1]+fib[i][j]/10000;//each of the above to save 10000, just at the beginning of each I have saved is 10, will be ultra-memory
fib[i][j]=fib[i][j]%10000;
}
}
}
int main ()
{
int n,i;
Fibonacci ();
while (scanf ("%d", &n)!=eof)
{
if (n==1| | n==2| | n==3| | N==4)//When the input data is a special case, the result can be output directly
{
printf ("1\n");
Continue
}
for (i=599;i>=0&&fib[n][i]==0;i--);//Eliminate the previous excess 0
printf ("%d", fib[n][i--]);//The output array is stored in the first few (<=4 bits), this bit to separate output can not be output in the following format, because the first few may have no need to output 0
for (; i>=0;i--)
printf ("%04d", Fib[n][i]);//%04d indicates that when outputting a numeric value less than 4 bits, it will be preceded by 0 to make its total width 4 bits, the output format must be so, otherwise wrong answer
printf ("\ n");
}
return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.