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 @   wojiaohuangyu  阅读(5)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示