程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU 1250 Hat's Fibonacci

HDU 1250 Hat's Fibonacci

編輯:C++入門知識

HDU 1250 Hat's Fibonacci


/*
解題人:lingnichong
解題時間:2014-10-18 23:48:54
解題體會:一開始用數組存一位的話顯得有點浪費,還容易超內存,數組開小了,又會顯示訪問到未知內存,可以每個數組的每個位上存上100000這樣大的數。
*/

Hat's Fibonacci

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

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.
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.

Author 戴帽子的



#include
#include
int a[8000][1000];
int main()
{
    int i,j,k,len,t,p;
    memset(a,0,sizeof(a));
    a[1][0]=1;a[2][0]=1;
	a[3][0]=1;a[4][0]=1;
    for(i=5;i<8000;i++)
    {
        for(j=1000-1;j>=0;j--)
        if(a[i-1][j]!=0)
        	break;
    	for(k=0;k<=j;k++)
		{
			a[i][k]+=(a[i-1][k]+a[i-2][k]+a[i-3][k]+a[i-4][k]);
			if(a[i][k]>=100000)//每一位數組存下100000,滿五位進一
			{
			    p=a[i][k];
				a[i][k]%=100000;
				a[i][k+1]+=(p/100000);
			} 
		}	
    }    
    while(~scanf("%d",&t))
    {
        for(i=1000-1;(i>=0)&&(a[t][i]==0);i--);
		printf("%d",a[t][i--]);//輸出一開始輸出的第一位數字,
        for(;i>=0;i--)
		printf("%.5d",a[t][i]);//後面每五位輸出次,因為是每五位進一 
		printf("\n");
    }
    return 0;
}    


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved