链接:

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. vue中通过路由跳转的三种方式

    原文:https://blog.csdn.net/qq_40072782/article/details/82533477 router-view 实现路由内容的地方,引入组件时写到需要引入的地方需要 ...

  2. jquery 实践操作:iframe 相关操作

    此篇记录关于HTML 的 iframe 元素 的相关记录 定义:iframe 元素会创建包含另外一个文档的内联框架(即行内框架). 常用的基本 iframe 设置(详细设置属性参考API:http:/ ...

  3. MYSQL常用的Show命令笔记

    1.查询数据库 SHOW DATABASES; 2.查询数据库中表 SHOW TABLES; SHOW TABLES FROM db; 3.查询表索引 SHOW INDEX FROM table 4. ...

  4. 安装聊天软件telegram-cli

    Telegram是一款加密通信的聊天软件,可以在linux,windows,android,chrome等运行.官方网址:https://telegram.org/ 它是有桌面版的,但作为一个linu ...

  5. hdu3715 2-sat+二分

    Go Deeper 题意:确定一个0/1数组(size:n)使得满足最多的条件数.条件在数组a,b,c给出. 吐槽:哎,一水提,还搞了很久!关键是抽象出题目模型(如上的一句话).以后做二sat:有哪些 ...

  6. java 修改字体大小

    在Windows->Preferences->General->Appearance->Colors and Fonts->Java->Java Editor Te ...

  7. With语句在数据统计应用

    WITH TMP_EXECUTOR(EXECUTOR,EXECUTORNAME) AS ( SELECT DISTINCT T.EXECUTOR ,T1.FULLNAME AS EXECUTORNAM ...

  8. DB2 数据库中字段特定字符替换为空

    Update RM_CarInfo set ImportTitle = Replace(ImportTitle,'ZD','') WHERE ImportTitle LIKE'%ZD%';

  9. 导入Excel表中的数据

    第一步:转换导入的文件 private void btnSelectFile_Click(object sender, EventArgs e) { OpenFileDialog ofd = new ...

  10. TP5 多条件whereOr查询

    问题背景:最近在用ThinkPHP 5开发项目的过程中,发现根据筛选条件做or查询的时候,连贯操作不可以使用where进行条件查询了. 首先列出一个user数据表的信息: uid uname grad ...