博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 1316 How Many Fibs?
阅读量:5926 次
发布时间:2019-06-19

本文共 2870 字,大约阅读时间需要 9 分钟。

**How Many Fibs?**Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5366    Accepted Submission(s): 2088Problem DescriptionRecall the definition of the Fibonacci numbers: 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]. InputThe 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 <= 10^100. The numbers a and b are given with no superfluous leading zeros.OutputFor each test case output on a single line the number of Fibonacci numbers fi with a <= fi <= b. Sample Input10 1001234567890 98765432100 0Sample Output54

题目大意:给你两个数a和b,求a和b之间有几个斐波那契数,包括a和b

解题思路:因为本题是一个很大的数,所以就用字符串,然后再加一个二分就好了。。。。

请看代码:

/*2015 - 8 - 13 下午Author: ITAK今日的我要超越昨日的我,明日的我要胜过今日的我,以创作出更好的代码为目标,不断地超越自己。*/#include 
#include
#include
using namespace std;const int N = 105;char data[1000][N+2];char a[N+2],b[N+2];int cmp(char *s1, char *s2){ for(int i=0; i<=N; i++) { if(i == N) return s1[i] - s2[i]; if(s1[i] != s2[i]) return s1[i] - s2[i]; }}int Find_up(int i, char *x){ int low=0, high=i; while(low <= high) { int mid = (high+low)/2; int val = cmp(x, data[mid]); if(val > 0) low = mid + 1; if(val == 0) return mid - 1; if(val < 0) high = mid - 1; } return high;}int Find_down(int i, char *x){ int low=0, high=i; while(low <= high) { int mid = (high+low)/2; int val = cmp(x, data[mid]); if(val > 0) low = mid + 1; if(val == 0) return mid + 1; if(val < 0) high = mid - 1; } return low;}int main(){ data[0][105] = 1; data[1][105] = 2; int i = 2; int p = 105; while(data[i-1][5] <= 1) { for(int j=105; j>=p; j--) data[i][j] = data[i-1][j] + data[i-2][j]; for(int j=105; j>=p; j--) { int c = data[i][j]/10; if(c > 0) { data[i][j] %= 10; data[i][j-1] += c; } } if(data[i][p-1] > 0) p--; i++; } while(~scanf("%s%s",a,b)) { if(a[0]=='0' && b[0]=='0') break; int lena = strlen(a)-1; int lenb = strlen(b)-1; int k; for(int d=lena,k=N; d>=0; d--,k--) { a[k] = a[d] - '0'; a[d] = 0; } for(int d=lenb,k=N; d>=0; d--,k--) { b[k] = b[d] - '0'; b[d] = 0; } int L = Find_up(i-1, a); int R = Find_down(i-1, b); printf("%d\n",R-L-1); memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); } return 0;}

转载地址:http://pqovx.baihongyu.com/

你可能感兴趣的文章
C#中字符串的内存分配与驻留池
查看>>
Cowboy 源码分析(十八)
查看>>
SharePoint 2007 "Select People and Groups"中搜索不到其他Domain账户的问题[已解决]
查看>>
cas 资源
查看>>
理中汤治疗口疮
查看>>
黄聪:wordpress如何开启文章格式post format
查看>>
Android文件Apk下载变ZIP压缩包解决方案
查看>>
[转载]Android Layout标签之-viewStub,requestFocus,merge,include
查看>>
Android菜单详解——理解android中的Menu
查看>>
[原] jQuery EasyUI 1.2.6源码、Demo合集、离线API
查看>>
view 背景透明
查看>>
基于mini2440的ov9650摄像头裸机测试
查看>>
HDU1702:ACboy needs your help again!
查看>>
对象androidandroid 开发中 如何取得ListView 的 每条Item 的对象
查看>>
怎样维护成功的开源项目
查看>>
mysql服务的启动和停止 net stop mysql net start mysql
查看>>
透过表象看本质!?之二数据拟合
查看>>
C语言数据结构----递归的应用(斐波拉契数列、汉诺塔、strlen的递归算法)
查看>>
ueditor 编辑器再thinkphp中使用 解决转义问题
查看>>
封装log4cp p
查看>>