HDU 1250 大数加斐波那契数列

Hat's Fibonacci

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4400    Accepted Submission(s): 1480

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.
 

先声明一个数组data,前四个数置1,后用大数加法实现4个数相加并打表,注意每位存放10000,一开始存放10,结果超内存

 1 /*
 2 练习简单DP是碰到的一道题,更像是大数相加
 3 先声明一个数组data,前四个数置1,后用大数加法实现4个数相加并打表,
 4 注意每位存放10000,一开始存放10,结果超内存
 5 */
 6 #include <stdio.h>
 7 #include <string.h>
 8 
 9 int data[7250][600];
10 int p;
11 int main()
12 {
13     memset(data,0,sizeof(data));
14     data[1][0]=1;
15     data[2][0]=1;
16     data[3][0]=1;
17     data[4][0]=1;
18     p=1;//各数种最高位所在加1
19     for(int i=5;i<=7200;i++)//循环计算各数
20     {
21         for(int j=0;j<=p;j++)
22            data[i][j]=data[i-1][j]+data[i-2][j]+data[i-3][j]+data[i-4][j];
23         for(int j=0;j<=p;j++)
24         {
25             data[i][j+1]+=data[i][j]/10000;
26             data[i][j]%=10000;
27         }
28 /*
29         printf("\n%d  \n",i);
30         for(int j=p;j>=0;j--)
31             printf("%d",data[i][j]);
32 */
33         if(data[i][p])
34             p++;
35     }
36 
37     int n;
38     //printf("p=%d\n",p);//衡量2003位数需要的i值
39     while(scanf("%d",&n)!=EOF)
40     {
41 
42         int i;
43 
44         for(i=p;i>=0;i--)
45             if(data[n][i]!=0)
46                 break;
47         printf("%d",data[n][i--]);//第一位不需要前置0
48        for(;i>=0;i--)
49        {
50            printf("%04d",data[n][i]);//不足4位前置0
51        }
52 
53         printf("\n");
54     }
55     return 0;
56 }

 

posted on 2013-01-16 10:48  行者1992  阅读(2079)  评论(0编辑  收藏  举报