链接:

pid=1316" target="_blank">点击打开链接

题意:问区间[a,b]中有多少斐波那契数

代码:

  1. #include <iostream>
  2. #include <string.h>
  3. #include <stdio.h>
  4. using namespace std;
  5. #define N 10000
  6. #define M 300
  7. char str[N][M];
  8. int cmp(char *a,char *b){
  9. int len1,len2;
  10. len1=strlen(a);
  11. len2=strlen(b);
  12. if(len1>len2)
  13. return 1;
  14. if(len1<len2)
  15. return -1;
  16. if(len1==len2){
  17. if(strcmp(a,b)==0)
  18. return 0;
  19. else if(strcmp(a,b)>0)
  20. return 1;
  21. else if(strcmp(a,b)<0)
  22. return -1;
  23. }
  24. } //由于斐波那契不是依照字符顺序排序,因此自写一个cmp函数
  25. void overthrow(char *s){
  26. int i,j;
  27. i=M-1;
  28. while(s[i]=='0')
  29. i--;
  30. s[i+1]='\0';
  31. for(j=0;j<=i/2;j++)
  32. swap(s[j],s[i-j]);
  33. } //由于是从左右往右加的,所以将高位和地位互换
  34. void bignumber(){
  35. long long i,j,t;
  36. str[1][0]='1';str[2][0]='2';
  37. for(i=3;i<N;i++){
  38. t=0;
  39. for(j=0;j<M;j++){
  40. t=t+str[i-1][j]-'0'+str[i-2][j]-'0';
  41. str[i][j]=t%10+'0';
  42. t/=10;
  43. }
  44. }
  45. } //大数斐波那契,注意是字符想加的时候要-'0'
  46. int binsearch1(char *s){
  47. int low,high,mid;
  48. low=1;high=N;
  49. while(low<=high){
  50. mid=(low+high)/2;
  51. if(cmp(str[mid],s)==0)
  52. return mid;
  53. else if(cmp(str[mid],s)>0)
  54. high=mid-1;
  55. else if(cmp(str[mid],s)<0)
  56. low=mid+1;
  57. }
  58. return low;
  59. } //返回比要查找的数较大的数的下标
  60. int binsearch2(char *s){
  61. int low,high,mid;
  62. low=1;high=N;
  63. while(low<=high){
  64. mid=(low+high)/2;
  65. if(cmp(str[mid],s)==0)
  66. return mid;
  67. else if(cmp(str[mid],s)>0)
  68. high=mid-1;
  69. else if(cmp(str[mid],s)<0)
  70. low=mid+1;
  71. }
  72. return high;
  73. } //返回比要查找的数较小的数的下标
  74. int main(){
  75. char a[305],b[305];
  76. int i,j,sum;
  77. for(i=1;i<N;i++)
  78. for(j=0;j<M;j++)
  79. str[i][j]='0'; //初始化为字符'0'
  80. bignumber();
  81. for(i=1;i<N;i++)
  82. overthrow(str[i]); //调用完bignumber(),之后翻转每个斐波那契数
  83. // for(i=1;i<=20;i++)
  84. // cout<<str[i]<<endl;
  85. while(cin>>a>>b){
  86. if(strcmp(a,"0")==0&&strcmp(b,"0")==0)
  87. break;
  88. // cout<<binsearch1(a)<<endl;
  89. // cout<<binsearch2(b)<<endl;
  90. sum=binsearch2(b)-binsearch1(a)+1; //不要忘记加1
  91. printf("%d\n",sum);
  92. }
  93. return 0;
  94. }

hdu1316的更多相关文章

  1. HDU1316(求区间斐波那契数的个数)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1316 题意:给两个数a和b,其中它们可能很大,最大到10^100,然后求去区间[a,b]内有多少个fib数 ...

  2. Java大数统计-hdu1316

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1316 题目描述: 给你一个范围,问你在这个范围内有多少斐波拉契数. 代码实现: import java ...

  3. (compareTo) How Many Fibs hdu1316 && ZOJ1962

    How Many Fibs? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  4. hdu1316(大数的斐波那契数)

    题目信息:求两个大数之间的斐波那契数的个数(C++/JAVA) pid=1316">http://acm.hdu.edu.cn/showproblem.php? pid=1316 这里 ...

  5. hdu1316 水大数

    题意:      给你一个区间,问这个区间有多少个斐波那契数. 思路:      水的大数,可以直接模拟,要是懒可以用JAVA,我模拟的,打表打到1000个就足够用了... #include<s ...

  6. hdu1316 大数

    题意:      给你一个区间,问这个区间有多少个斐波那契数. 思路:      水的大数,可以直接模拟,要是懒可以用JAVA,我模拟的,打表打到1000个就足够用了... #include<s ...

  7. HDU中大数实现的题目,持续更新(JAVA实现)

    HDU1002:大数加法,PE了N次 import java.util.Scanner; import java.math.*; public class Main { public static v ...

  8. 大数问题,通常用JAVA

    e.g. HDU1002 简单加法 import java.math.BigInteger; import java.util.Scanner; public class Main { public ...

随机推荐

  1. 【03】react 之 创建component

    React推出后,出于不同的原因先后出现三种定义react组件的方式,殊途同归:具体的三种方式: 函数式定义的无状态组件 es5原生方式React.createClass定义的组件 es6形式的ext ...

  2. NKOI 1469 通向自由的钥匙

    P1469通向自由的钥匙 时间限制 : 10000 MS   空间限制 : 65536 KB 问题描述 通向自由的钥匙被放n个房间里,这n个房间由n-1条走廊连接.但是每个房间里都有特别的保护魔法,在 ...

  3. 【UVA11859】Division Game(SG函数,Nim游戏)

    题意:给定一个n*m的矩阵,两个游戏者轮流操作. 每次可以选一行中的1个或多个大于1的整数,把它们中的每个数都变成它的某个真因子,不能操作的输. 问先手能否获胜 n,m<=50,2<=a[ ...

  4. POJ 3090

    Visible Lattice Points Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8397   Accepted: ...

  5. config .net webapi to return json.

    1.add content negotiator using System; using System.Collections.Generic; using System.Linq; using Sy ...

  6. 5种你未必知道的JS和CSS交互的方法

    随着浏览器不断的升级改进,CSS和JavaScript之间的界限越来越模糊.本来它们是负责着完全不同的功能,但最终,它们都属于网页前端技术,它们需要相互密切的合作.我们的网页中都有.js文件和.css ...

  7. poj 3693 Maximum repetition substring 重复次数最多的连续子串

    题目链接 题意 对于任意的字符串,定义它的 重复次数 为:它最多可被划分成的完全相同的子串个数.例如:ababab 的重复次数为3,ababa 的重复次数为1. 现给定一字符串,求它的一个子串,其重复 ...

  8. 《30天学习30种新技术》-Day 15:Meteor —— 从零开始创建一个 Web 应用

    目录:https://segmentfault.com/a/1190000000349384 原文: https://segmentfault.com/a/1190000000361440 到目前为止 ...

  9. hdu 4524(模拟)

    郑厂长系列故事——逃离迷宫 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  10. LeetCode OJ-- Combination Sum II **

    https://oj.leetcode.com/problems/combination-sum-ii/ 一列数,每个数只能用一次或者不用,给出和为target的组合. 递归写的深搜,使用了编程技巧, ...