1293: 大斐波数(acm.zzuli.edu.cn)
来源:互联网 发布:杜兰特数据统计库 编辑:程序博客网 时间:2024/06/05 19:34
Description
Fibonacci数列,定义如下:
f(1)=f(2)=1
f(n)=f(n-1)+f(n-2) n>=3。
计算第n项Fibonacci数值。
Input
输入第一行为一个整数N,接下来N行为整数Pi(1<=Pi<=1000)。
Output
输出为N行,每行为对应的f(Pi)。
Sample Input
5
1
2
3
4
5
Sample Output
1
1
2
3
5
HINT
此题需要用大数处理(string 处理)
这里大致说一下解题思路: 这道题跟以往写过的 斐波那契数列题不一样,以前的大多都是要求结果对某个素数取余,这道题是直接让你输出,所以对于大数据的处理一般想到的就是用字符串来解决该问题了,这道题只有用到了大整数运算中的 正整数加法,另外,程序中,我还用了map打了一个大斐波数表。整体的思路很简单,代码如下:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <map>using namespace std;const int N = 1000;void Reverse(char *str){ char temp[N]; int len = strlen(str); int k = 0; strcpy(temp, str); for(int i = len-1; i >= 0; i--) { str[k++] = temp[i]; } str[k] = '\0';}char * BIA(char * a, char *b){ int a_len = strlen(a); int b_len = strlen(b); int maxlen = max(a_len, b_len); int m = 0, n = 0; //进位 int c = 0, i; char *ans = new char[maxlen + 2]; char *pa = a + a_len - 1; char *pb = b + b_len - 1; for(i = 0; i < maxlen; i++) { m = n = 0; if(pa + 1 != a) { m = *pa - 48; pa--; } if(pb + 1 != b) { n = *pb - 48; pb--; } *(ans+i) = (m + n + c) % 10 + 48; c = (m + n + c) / 10; } if(c > 0) { *(ans + i) = c + 48; *(ans + i + 1) = '\0'; } else *(ans + i) = '\0'; Reverse(ans); return ans;}int main(){ // 打表 char s1[N] = "1"; char s2[N] = "1"; char *s3 = new char[N]; map<int, string> sky; sky.clear(); sky[1] = sky[2] = s1; for(int i = 3; i <= 1000; i++) { s3 = BIA(s1, s2); sky[i] = s3; strcpy(s1, s2); strcpy(s2, s3); } int t; cin>>t; while(t--) { int num; scanf("%d", &num); cout<<sky[num]<<endl; } return 0;}
阅读全文
0 0
- 1293: 大斐波数(acm.zzuli.edu.cn)
- Post office (区间dp 记忆化搜索) http://acm.zzuli.edu.cn/problem.php?id=1468
- acm.jlu.edu.cn-1097-System Overload
- acm.jlu.edu.cn-1071-Maya Calendar
- acm.jlu.edu.cn-1317-Tree Recovery
- acm.jlu.edu.cn-1099-Simple Computers
- acm.jlu.edu.cn-1068-Cipher
- Problems by Topic @ acm.pku.edu.cn
- 1010(stamps),acm.pku.edu.cn
- http://acm.pku.edu.cn/JudgeOnline/
- acm.jlu.edu.cn-2098-All in All
- acm.jlu.edu.cn-2005-The Dir Command
- acm.jlu.edu.cn-1552-Identifying Legal Pascal Real Constants
- hdu1312http://acm.hdu.edu.cn/showproblem.php?pid=1312
- http://acm.hdu.edu.cn/showproblem.php?pid=1005
- http://acm.hdu.edu.cn/showproblem.php?pid=1333
- http://acm.hdu.edu.cn/showproblem.php?pid=2710
- http://acm.hdu.edu.cn/showproblem.php?pid=2866
- log4j.properties配置详解与实例-全部测试通过
- Reshape the Matrix
- 阿里云技术教程系列-ECS创建 Linux 实例
- 习题6.1(2)
- Python使用matplotlib,numpy,scipy进行散点的平滑曲线化方法
- 1293: 大斐波数(acm.zzuli.edu.cn)
- Unsupervised Feature Selection in Signed Social Networks 阅读笔记
- tiny4412开发板LED灯驱动写法
- Eclipse上安装svn插件及解决Fail to load JavaHL Library的错误
- 使用for及递归求 1-100的和
- php出现错误Can't use function return value in write
- PHP require和include区别
- JavaShowAlgorithm-Binary Tree Convert double direction LinkedList
- 我悄悄告诉你:怎样用jquery制作腾讯QQ空间说说发表效果