How many Fibs?(高精度)
来源:互联网 发布:php 异步请求 编辑:程序博客网 时间:2024/06/12 19:26
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
解题思路
先打个斐波那契的表,我打了480个,感觉高精度的题必须自己动手多操作,细节很多,看别人的代码只要看个思路,细看就不值得了,细节还是自己操作时,一步步调试,
下面的代码写得略拙-_-!,,本来是用二分查找的,楸了半天没楸好,过两天再试试。
特注:### strcmp()函数并不是先比较字符串长度的,而是直接相减 。
AC代码
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxnlen = 210;char fib[485][maxnlen]; //存放斐波那契数列void turn(char *a) //定义倒序函数{ int L; char temp[maxnlen]; L=strlen(a); for(int i=L-1, j=0; i>=0; i--, j++) temp[j]=a[i]; strcpy(a, temp); a[L] = '\0' ;}void get_fibs(){ int pp, i, j; strcpy(fib[1], "1"); strcpy(fib[2], "2"); for(i=3; i<485; i++) { pp = 0; int L1 = strlen( fib[i-2] ); int L2 = strlen( fib[i-1] ); for(j=0; j<L1; j++) { fib[i][j] = (fib[i-2][j] - '0' + fib[i-1][j] - '0' + pp) % 10 + '0' ; pp = (fib[i-2][j] - '0' + fib[i-1][j] - '0' + pp) / 10 ; } if( L1 < L2 ) fib[i][j] = (fib[i-1][j] - '0' + pp) + '0' ; else if( L1==L2 && pp == 1 ) fib[i][j] = '1' ; //fib[]中一直是倒序的 } for(int i=1; i<485; i++) turn( fib[i] ); //转为正序}int main(){ get_fibs(); int i, j, k, flag1, flag2, flag=0; char a[maxnlen], b[maxnlen]; while( scanf("%s%s", a, b) ) //有待优化的查找!!! { if( strcmp(a, "0")==0 && strcmp(b, "0")==0 ) break; for(i = 1; i < 500; i++) { if(strlen(fib[i]) > strlen(a)) { flag1 = i; break; } if(strlen(fib[i]) == strlen(a) && strcmp(fib[i], a) == 0) { flag1 = i; flag = 1; break; } if(strlen(fib[i]) == strlen(a) && strcmp(fib[i], a) > 0) { flag1 = i; break; } } for(i = 1; i < 500; i++) { if(strlen(fib[i]) > strlen(b)) { flag2 = i; break; } if(strlen(fib[i]) == strlen(b) && strcmp(fib[i], b) == 0) { flag2 = i; if(flag == 1) flag2++; break; } if(strlen(fib[i]) == strlen(b) && strcmp(fib[i], b) > 0) { flag2 = i; break; } } printf("%d\n", flag2 - flag1); } 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)大数
- uva 11992 Fast Matrix Operations 线段树
- 色彩转换
- 有哪些可供Web设计师&开发者消遣的网站?
- 视频帧人脸检测 (I)
- Android Junit测试框架的配置
- How many Fibs?(高精度)
- poj 1611(详解)
- 黑马程序员:银行业务调度系统
- 软件质量度量参考数据表
- zoj2314 Reactor Cooling --- 上下界可行流
- LFS之(1)建立新分区并挂载
- j2ee 实现
- hdu 2191 多重背包
- qqlive播放器下载视频