hdu-1250

Hat's Fibonacci

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


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.
 第一次做这道题目的时候不知道怎么做
不过做过这道题目之后我又涨姿势了
原来大数题还可以这么做的
这道题目就是斐波数,只不过数比较大
代码如下:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int dashu[20000][360];
int main(){
int i,j,k,t;
memset(dashu,0,sizeof(dashu));
dashu[1][0]=1;
dashu[2][0]=1;
dashu[3][0]=1;
dashu[4][0]=1;
for(i=5;i<20000;i++){
int num=0;
for(j=0;j<360;j++){
dashu[i][j]=dashu[i-1][j]+dashu[i-2][j]+dashu[i-3][j]+dashu[i-4][j]+num;
  num=dashu[i][j]/100000000;
  dashu[i][j]%=100000000;
 }
}
int n;
while(~scanf("%d",&n)){
for(i=359;i>=0;i--){
if(dashu[n][i]){
printf("%d",dashu[n][i]);        //排除为0的 
i--;
break;
}
}
for(;i>=0;i--)
  printf("%08d",dashu[n][i]);        //需要以08d结尾,不然的话,就会将0忽略 
printf("\n");
}
return 0;
} 


posted @ 2014-10-24 14:56  wojiaohuangyu  阅读(10)  评论(0)    收藏  举报