链接:

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

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

代码:

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
#define N 10000
#define M 300
char str[N][M];
int cmp(char *a,char *b){
int len1,len2;
len1=strlen(a);
len2=strlen(b);
if(len1>len2)
return 1;
if(len1<len2)
return -1;
if(len1==len2){
if(strcmp(a,b)==0)
return 0;
else if(strcmp(a,b)>0)
return 1;
else if(strcmp(a,b)<0)
return -1;
}
} //由于斐波那契不是依照字符顺序排序,因此自写一个cmp函数
void overthrow(char *s){
int i,j;
i=M-1;
while(s[i]=='0')
i--;
s[i+1]='\0';
for(j=0;j<=i/2;j++)
swap(s[j],s[i-j]);
} //由于是从左右往右加的,所以将高位和地位互换
void bignumber(){
long long i,j,t;
str[1][0]='1';str[2][0]='2';
for(i=3;i<N;i++){
t=0;
for(j=0;j<M;j++){
t=t+str[i-1][j]-'0'+str[i-2][j]-'0';
str[i][j]=t%10+'0';
t/=10;
}
}
} //大数斐波那契,注意是字符想加的时候要-'0'
int binsearch1(char *s){
int low,high,mid;
low=1;high=N;
while(low<=high){
mid=(low+high)/2;
if(cmp(str[mid],s)==0)
return mid;
else if(cmp(str[mid],s)>0)
high=mid-1;
else if(cmp(str[mid],s)<0)
low=mid+1;
}
return low;
} //返回比要查找的数较大的数的下标
int binsearch2(char *s){
int low,high,mid;
low=1;high=N;
while(low<=high){
mid=(low+high)/2;
if(cmp(str[mid],s)==0)
return mid;
else if(cmp(str[mid],s)>0)
high=mid-1;
else if(cmp(str[mid],s)<0)
low=mid+1;
}
return high;
} //返回比要查找的数较小的数的下标
int main(){
char a[305],b[305];
int i,j,sum;
for(i=1;i<N;i++)
for(j=0;j<M;j++)
str[i][j]='0'; //初始化为字符'0'
bignumber();
for(i=1;i<N;i++)
overthrow(str[i]); //调用完bignumber(),之后翻转每个斐波那契数
// for(i=1;i<=20;i++)
// cout<<str[i]<<endl;
while(cin>>a>>b){
if(strcmp(a,"0")==0&&strcmp(b,"0")==0)
break;
// cout<<binsearch1(a)<<endl;
// cout<<binsearch2(b)<<endl;
sum=binsearch2(b)-binsearch1(a)+1; //不要忘记加1
printf("%d\n",sum);
}
return 0;
}

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. 【04】Vue 之 事件处理

    4.1. 监听事件的Vue处理 Vue提供了协助我们为标签绑定时间的方法,当然我们可以直接用dom原生的方式去绑定事件.Vue提供的指令进行绑定也是非常方便,而且能让ViewModel更简洁,逻辑更彻 ...

  2. python斐波那契数列复杂度

    契数列 概述: 斐波那契数列,又称黄金分割数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21.34.……在数学上,斐波纳契数列以如下被以递归的方法定义:F(0)=0,F(1)=1,F(n ...

  3. g++的编译选项:-Wl,-rpath=【转】

    转自:http://blog.csdn.net/russule/article/details/7057398 动态链接库 1 生成hello.so g++ -shared hello.cpp -ol ...

  4. fastboot flash image usage

    // // power on mobile phone // power on mobile phone // // reboot bootloader // android/out/host/lin ...

  5. 【spring专题】spring简介

    前景概要 对于现在的Java开发基本上可以说成是spring开发,spring全家桶可以说是把整个Java web安排的明明白白的.正因为使用的很多,所以作为一名开发者不应该仅仅是会使用spring, ...

  6. 八、 Java程序初始化的顺序(一)

    今天在写构造器方法的时候,遇到了一个小问题,由这个问题引发了一连串的思考,在一个Java类中变量与类的初始化执行顺序是什么样的呢?## 发现问题 class Student{ private Stri ...

  7. python3模拟扑克牌

    python3.6环境 import collections from random import choice Card=collections.namedtuple('Card',['rank', ...

  8. (5)ASP.NET HTML服务器控件

    工具箱 与服务端交互 <body> <form id="form1" runat="server"> <div> <% ...

  9. Codeforces Round #324 (Div. 2) Kolya and Tanya 组合数学

    原题链接:http://codeforces.com/contest/584/problem/B 题意: 有3*n个人围成一个圈,每个人可以分配1到3个硬币,但是相邻为n的三个人的和不能是6,问你有多 ...

  10. Linux文件名命名规范

    注意:在Linux下全部文件和文件夹命名都是小写的! Linux系统区分英文字符的大小写.命名目录和命名文件的规则是相同的.除非有特别的原因,否则用户创建的文件和目录名要使用小写字符.大多数的Linu ...