hdu 1250 Hat's Fibonacci(高精度加法)
2015-08-13 16:41
197 查看
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1250
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
Sample Output
2005位的话用高精度计算,算到第8000多应该就够了。
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.
2005位的话用高精度计算,算到第8000多应该就够了。
#include <iostream> #include <cstdio> using namespace std; const int maxn=2008; char f[9000][maxn+2]; //为什么取9000? int main() { int i=5,p=maxn,n,num; f[1][maxn]=f[2][maxn]=f[3][maxn]=f[4][maxn]=1; while(f[i-1][1]<=1){ for(int j=maxn;j>=p;j--){ f[i][j]=f[i-1][j]+f[i-2][j]+f[i-3][j]+f[i-4][j]; //****** } for(int j=maxn;j>=p;j--){ int c=f[i][j]/10; if(c>0){ f[i][j]=f[i][j]%10; f[i][j-1]+=c; } } if(f[i][p-1]>0)p--; i++; } while(cin>>n){ for(int k=0;k<=maxn;k++){ if(f [k]!=0){ num=k; break; } } for(int k=num;k<=maxn;k++)printf("%d",f [k]); puts(""); } return 0; }
相关文章推荐
- 插入排序
- hdu5389(DP)
- vmware虚拟机Windows 2003上网问题
- 第8章 用户模式下的线程同步(3)_Slim读写锁(SRWLock)
- 用JS改变的元素CSS样式
- Clustering Tips
- 学密码学一定得学程序
- 【bzoj1003】【ZJOI2006】【物流运输】【dp+最短路】
- ffmpeg生成带关键帧头信息的flv文件(一)
- pat(A) 1062. Talent and Virtue(结构体排序)
- iOS 获取本地设备相关信息,如:IP地址
- OC思维导图之NSString&NSMutableString
- 124. Binary Tree Maximum Path Sum
- java考核完的心得
- 数据库基本sql语句
- Android中BaseAdapter的理解
- 【leetcode】Rectangle Area【java】
- 推荐:Android酷炫实用的开源框架(UI框架)
- 利用存储过程动态创建表格
- [置顶] android源码编译apk集成第三方so库