hdu1316
链接: 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的更多相关文章
- HDU1316(求区间斐波那契数的个数)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1316 题意:给两个数a和b,其中它们可能很大,最大到10^100,然后求去区间[a,b]内有多少个fib数 ...
- Java大数统计-hdu1316
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1316 题目描述: 给你一个范围,问你在这个范围内有多少斐波拉契数. 代码实现: import java ...
- (compareTo) How Many Fibs hdu1316 && ZOJ1962
How Many Fibs? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- hdu1316(大数的斐波那契数)
题目信息:求两个大数之间的斐波那契数的个数(C++/JAVA) pid=1316">http://acm.hdu.edu.cn/showproblem.php? pid=1316 这里 ...
- hdu1316 水大数
题意: 给你一个区间,问这个区间有多少个斐波那契数. 思路: 水的大数,可以直接模拟,要是懒可以用JAVA,我模拟的,打表打到1000个就足够用了... #include<s ...
- hdu1316 大数
题意: 给你一个区间,问这个区间有多少个斐波那契数. 思路: 水的大数,可以直接模拟,要是懒可以用JAVA,我模拟的,打表打到1000个就足够用了... #include<s ...
- HDU中大数实现的题目,持续更新(JAVA实现)
HDU1002:大数加法,PE了N次 import java.util.Scanner; import java.math.*; public class Main { public static v ...
- 大数问题,通常用JAVA
e.g. HDU1002 简单加法 import java.math.BigInteger; import java.util.Scanner; public class Main { public ...
随机推荐
- RSA DSA
RSA https://blog.csdn.net/sunmenggmail/article/details/11994013 https://baike.baidu.com/item/RSA%E7% ...
- 在VS2013中使用boost库遇到的问题及解决(转)
原文转自 https://my.oschina.net/SunLightJuly/blog/676891?p=1 最近的项目需要集成一个使用了boost库的开源库.原本应该是比较简单的工作,因为使用的 ...
- Java学习之路(3)
JKD的安装和配置: 一.适合超级菜鸟的,不用设置系统路径,不用到处找插件 (1)如果你的系统是windows或linux,根据系统的不同(32位和64位之分),先到www.baidu.com搜索jd ...
- 涂色问题(Python)
题目:将一个圆形等分成N个小扇形,将这些扇形标记为1,2,3,-,N.现在使用M种颜色对每个扇形进行涂色,每个扇形涂一种颜色,且相邻的扇形颜色不同,问有多少种不同的涂法?(N≥1,M≥3) 参考:ht ...
- 转:C#制作ORM映射学习笔记二 配置类及Sql语句生成类
在正式开始实现ORM之前还有一点准备工作需要完成,第一是实现一个配置类,这个很简单的就是通过静态变量来保存数据库的一些连接信息,等同于.net项目中的web.config的功能:第二需要设计实现一个s ...
- LeetCode OJ--4Sum *
https://oj.leetcode.com/problems/4sum/ 在一个数列中,找出所有的4个数,它们的和是target. class Solution { public: vector& ...
- 机器学习3_EM算法与混合高斯模型
①EM算法: http://www.cnblogs.com/jerrylead/archive/2011/04/06/2006936.html 李航 <统计学习方法>9.1节 ②混合高斯模 ...
- Python操作sqlite数据库小节
学习了Python操作sqlite数据库,做一个小结,以备后用. import sqlite3import os# 进行数据库操作时,主要是参数如何传输try:# 链接数据库conn=sqlite3. ...
- MFC中 获取新输入编辑框的内容
//得到原始内容的长度 int len = m_editPoemFileStr.GetLength(); UpdateData(true); //得到新增加的内容 CString sNewStrin ...
- Spark HA模式访问Hadoop HA下的数据
首先是需要将hadoop的配置文件core-site,xml和hdfs-site.xml 拷贝到Spark conf目录下 然后启动提交即可 spark-submit \ --master spark ...