HDU 1316 (斐波那契数列,大数相加,大数比较大小)
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1316
Recall 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].
#include<bits/stdc++.h>
using namespace std;
string str[];
string add(string str1,string str2)//大数相加
{
int l1=str1.length();
int l2=str2.length();
if(l1>l2)
{
for(int i=;i<l1-l2;i++)
{
str2=""+str2;
}
}else if(l1<l2)
{
for(int i=;i<l2-l1;i++)
{
str1=""+str1;
}
}
l1=str1.length();
string str3;
int c=;
for(int i=l1-;i>=;i--)
{
int temp=str1[i]-''+str2[i]-''+c;
c=temp/;
temp=temp%;
str3=char(temp+'')+str3;
}
if(c!=)
{
str3=char(c+'')+str3;
}
return str3;
}
int compare(string str1,string str2)//str1大于等于str2,返回1
{
int l1=str1.length();
int l2=str2.length();
if(l1>l2)
{
return ;
}else if(l1<l2)
{
return ;
}else
{
for(int i=;i<l1;i++)
{
if(str1[i]>str2[i])
{
return ;
}else if(str1[i]==str2[i])
{
continue;
}else
{
return ;
}
}
}
return ;
}
int f(string str1,string str2)//找出两个大数中间的斐波那契数的个数,包括边界
{
int l1=str1.length(),l2=str2.length(),index1,index2;
for(int i=;i<=;i++)
{
int k=str[i].length();
if(k<l1)
continue;
else
{
index1=i;
break;
}
}
for(int i=;i>=;i--)
{
int k=str[i].length();
if(k>l2)
continue;
else
{
index2=i;
break;
}
}
int r=;
for(int i=index1;i<=index2;i++)
{
if(compare(str[i],str1)==&&compare(str2,str[i])==)
{
r++;
}
}
return r;
}
int main()
{
string str1,str2;
str[]="";
str[]="";
for(int i=;i<;i++)//先求出需要的斐波那契数列,第500个斐波那契数大于10的1000次方
{
str[i]=add(str[i-],str[i-]);
}
while(cin>>str1>>str2)
{
if(str1==""&&str2=="")
break;
int r=f(str1,str2);
printf("%d\n",r);
}
return ;
}
HDU 1316 (斐波那契数列,大数相加,大数比较大小)的更多相关文章
- hdu 5914(斐波拉契数列)
Triangle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- HDU——4549M斐波那契数列(矩阵快速幂+快速幂+费马小定理)
M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Su ...
- HDU 5686 斐波那契数列、Java求大数
原题:http://acm.hdu.edu.cn/showproblem.php?pid=5686 当我们要求f[n]时,可以考虑为前n-1个1的情况有加了一个1. 此时有两种情况:当不适用第n个1进 ...
- D - 1sting(相当于斐波那契数列,用大数写)
Description You will be given a string which only contains ‘1’; You can merge two adjacent ‘1’ to be ...
- HDU 1715 斐波那契数列1000项
二维数组模拟大数加法就可以了,不太难,直接上代码了. #include<stdio.h> #include<string.h> #include<math.h> # ...
- HDU 1715 (大数相加,斐波拉契数列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1715 大菲波数 Time Limit: 1000/1000 MS (Java/Others) ...
- java大数 斐波那契数列
java大数做斐波那契数列: 思路:1. 2.可以用数组存着 import java.math.BigInteger; import java.util.Scanner; public ...
- P2626 斐波那契数列(升级版)(合数的质数分解, 大数为素数的概率十分小的利用)
题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: f(1)=1f(1) = 1 f(1)=1 f(2)=1f(2) = 1f(2)=1 f(n)=f(n−1)+f(n−2)f(n) = f ...
- hdu number number number 斐波那契数列 思维
http://acm.hdu.edu.cn/showproblem.php?pid=6198 F0=0,F1=1的斐波那契数列. 给定K,问最小的不能被k个数组合而成的数是什么. 赛后才突然醒悟,只要 ...
随机推荐
- Layabox 3D游戏开发学习笔记---射线检测,鼠标控制物体运动
核心要点:3D物体碰撞是靠射线检测,射线与碰撞器相撞获取对应的碰撞点信息. class RayPicking03 { private ray: Laya.Ray; private point: Lay ...
- 在MyBatis中查询数据、涉及多参数的数据访问操作、插入数据时获取数据自增长的id、关联表查询操作、动态SQL、关于配置MyBatis映射没有代码提示的解决方案
1. 单元测试 在单元测试中,每个测试方法都需要执行相同的前置代码和后置代码,则可以自定义2个方法,分别在这2个方法中执行前置代码和后置代码,并为这2个方法添加@Before和@After注解,然后, ...
- P/Invoke出现错误 System.NullReferenceException”类型的未经处理的异常在 未知模块。 中发生 未将对象引用设置到对象的实例。
问题 “System.NullReferenceException”类型的未经处理的异常在 未知模块. 中发生 未将对象引用设置到对象的实例. 解决方案 1.尝试 用管理员身份运行CMD,输入nets ...
- ubuntu下创建虚拟python3开发环境
友情链接:ubuntu16.04下安装python3+创建虚拟python3开发环境 1.为什么要创建python3虚拟开发环境? /********************************* ...
- kafka介绍 - 官网
介绍 Kafka是一个分布式的.分区的.冗余的日志提交服务.它使用了独特的设计,提供了所有消息传递系统所具有的功能. 我们先来看下几个消息传递系统的术语: Kafka维护消息类别的东西是主题(topi ...
- jquery中的ajax方法
$.ajax()方法详解 jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为Strin ...
- 【Oracle】存储过程写法小例子
1.存储过程的基本语法: CREATE OR REPLACE PROCEDURE 存储过程名(param1 in type,param2 out type) IS 变量1 类型(值范围); 变量2 类 ...
- [翻译] CHTCollectionViewWaterfallLayout
CHTCollectionViewWaterfallLayout https://github.com/chiahsien/CHTCollectionViewWaterfallLayout CHTCo ...
- 使用CoreData [1]
使用CoreData [1] 本篇教程能教你从无开始接触CoreData,包括新建工程,创建出实体,增删改查样样都有,属于使用CoreData最初级教程. 1. 创建带有CoreData的工程项目 2 ...
- Linux下安装Tomcat7
一.Tomcat7软件包下载 Tomcat下载地址http://tomcat.apache.org/download-70.cgi 下载完成后, 将软件包apache-tomcat-7.0.82.ta ...