How many Fibs?(高精度)
来源:互联网 发布:淘宝客自动采集推广 编辑:程序博客网 时间:2024/06/07 00:35
Description
Recall the definition of the Fibonacci numbers:
Given two numbers a and b, calculate how many Fibonacci numbers are in the range [a,b].
f1 := 1 f2 := 2 fn := fn-1 + fn-2 (n>=3)
Given two numbers a and b, calculate how many Fibonacci numbers are in the range [a,b].
Input
The input contains several test cases. Each test case consists of two non-negative integer numbers a and b. Input is terminated by a=b=0. Otherwise, a<=b<=10100. The numbers a and b are given with no superfluous leading zeros.
Output
For each test case output on a single line the number of Fibonacci numbers fi with a<=fi<=b.
Sample Input
10 1001234567890 98765432100 0
Sample Output
54
解题思路:
题目大意是给个区间,问这区间内有多少个斐波那契数。区间给的很大,得用高精度来求。先把斐波那契数求出来,再根据区间进行查找,我求了前两千个斐波那契数,反正是绝对够了。只有查找我发现自己十分不机智了。竟然自己辛辛苦苦写了个比较函数,而且还是非常龊那种。完全可以把斐波那契数存入字符串中,由于字符串的比较函数是按照字典序来比较的,正符合我的需求。用字符串的比较函数就方便多了。哎!太TMD不机智了!!!!不过不想再写一次了,反正能AC就行,就不再优化了。。。。我的代码就像老太婆的裹脚布,又臭又长,有木有!有木有!
AC代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn = 500;int fib[2000][maxn];void Fibs(){ memset(fib, 0, sizeof(fib)); fib[1][0] = 1; fib[2][0] = 2; for(int i = 3; i < 1500; i++) { int ans = 0; for(int j = 0; j < maxn; j++) { fib[i][j] = fib[i - 1][j] + fib[i - 2][j] + ans; ans = fib[i][j] / 10; fib[i][j] %= 10; } }}int main(){ char str_1[maxn], str_2[maxn]; int num_1[maxn], num_2[maxn], length_1, length_2; Fibs(); while(scanf("%s%s", str_1, str_2)) { int begin, end, len_1, len_2; if(str_1[0] == '0' && str_2[0] == '0') break; length_1 = strlen(str_1); length_2 = strlen(str_2); for(int i = 0; i < length_1; i++) num_1[i] = str_1[length_1 - i - 1] - '0'; for(int i = 0; i < length_2; i++) num_2[i] = str_2[length_2 - i - 1] - '0'; for(int i = 1; i < 1500; i++) { bool isFind = false; for(int j = maxn - 1; j >= 0; j--) { if(fib[i][j]) { len_1 = j + 1; break; } } if(len_1 < length_1) continue; if(len_1 > length_1) { begin = i; break; } for(int j = len_1 - 1; j >= 0; j--) { if(j == 0 && fib[i][j] == num_1[j]) isFind = true; if(fib[i][j] == num_1[j]) continue; if(fib[i][j] > num_1[j]) isFind = true; break; } if(isFind) { begin = i; break; } } for(int i = 1499; i > 0; i--) { bool isFind = false; for(int j = maxn - 1; j >= 0; j--) { if(fib[i][j]) { len_2 = j + 1; break; } } if(len_2 > length_2) continue; if(len_2 < length_2) { end = i; break; } for(int j = len_2 - 1; j >= 0; j--) { if(j == 0 && fib[i][j] == num_2[j]) isFind = true; if(fib[i][j] == num_2[j]) continue; if(fib[i][j] < num_2[j]) isFind = true; break; } if(isFind) { end = i; break; } } printf("%d\n",end - begin + 1); } return 0;}
0 0
- How many Fibs?(高精度)
- How many Fibs?(高精度)
- How Many Fibs? hdu1316 高精度
- hdu1316(How Many Fibs?)高精度
- JAVA hdu 1316 How Many Fibs?(高精度)
- POJ 2413 How many Fibs?(高精度暴力)
- pku 2413 How many Fibs? 【java高精度】
- hdu 1316 How Many Fibs? (模拟高精度)
- POJ2413 How many Fibs?【高精度】【二分】
- How Many Fibs? 【java 处理高精度】
- POJ2413 How many Fibs(高精度)(AC)
- hdu 1316 How Many Fibs?(高精度加法+ 斐波那契数)
- UVA 10183How Many Fibs?(高精度 斐波那契)
- UVA - 10183 - How Many Fibs? (斐波那契 + 高精度)
- hdu 1316 How Many Fibs?(Fibonacci+高精度+二分)
- hdu1316 How Many Fibs?(大数)
- (挑战编程_6_1)How many Fibs?
- How many Fibs?(JAVA)大数
- bootstrap学习——javascript插件篇
- A+B for Input-Output Practice (IV)
- 蓝牙用例
- 应用服务器-tomcat内存设置问题
- Python学习26:类属性vs实例属性
- How many Fibs?(高精度)
- C/C++回调函数
- 聘请的Java开发人员利用Java程序设计需求
- 1124:成语接龙 dfs+一维数组保存结果
- 鹰目LED联播网全线出击 助您“屏”地而起
- Hibernate继承映射
- 导弹拦截系统
- SSH整合中 hibernate整合操作
- wpf——数据绑定DataContext