题目链接:

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].

 
Input
The input contains several test cases. Each test case consists of two non-negative integer numbers a and b. Input is terminated by a = b = 0. Otherwise, a <= b <= 10^100. The numbers a and b are given with no superfluous leading zeros.
 
Output
For each test case output on a single line the number of Fibonacci numbers fi with a <= fi <= b.
 
Sample Input
10 100
1234567890 9876543210
 0 0
 
Sample Output
5
4
 
代码如下:
#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 (斐波那契数列,大数相加,大数比较大小)的更多相关文章

  1. hdu 5914(斐波拉契数列)

    Triangle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  2. HDU——4549M斐波那契数列(矩阵快速幂+快速幂+费马小定理)

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Su ...

  3. HDU 5686 斐波那契数列、Java求大数

    原题:http://acm.hdu.edu.cn/showproblem.php?pid=5686 当我们要求f[n]时,可以考虑为前n-1个1的情况有加了一个1. 此时有两种情况:当不适用第n个1进 ...

  4. D - 1sting(相当于斐波那契数列,用大数写)

    Description You will be given a string which only contains ‘1’; You can merge two adjacent ‘1’ to be ...

  5. HDU 1715 斐波那契数列1000项

    二维数组模拟大数加法就可以了,不太难,直接上代码了. #include<stdio.h> #include<string.h> #include<math.h> # ...

  6. HDU 1715 (大数相加,斐波拉契数列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1715 大菲波数 Time Limit: 1000/1000 MS (Java/Others)     ...

  7. java大数 斐波那契数列

    java大数做斐波那契数列:  思路:1.       2.可以用数组存着 import java.math.BigInteger; import java.util.Scanner; public ...

  8. 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 ...

  9. hdu number number number 斐波那契数列 思维

    http://acm.hdu.edu.cn/showproblem.php?pid=6198 F0=0,F1=1的斐波那契数列. 给定K,问最小的不能被k个数组合而成的数是什么. 赛后才突然醒悟,只要 ...

随机推荐

  1. 【代码笔记】iOS-自动成表格的效果

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> #import "LabelOnBackImag ...

  2. input pattern中常用的正则表达式

    常用的正则表达式 pattern的用法,只是列出来一些常用的正则: 信用卡 [0-9]{13,16} 银联卡 ^62[0-5]\d{13,16}$ Visa: ^4[0-9]{12}(?:[0-9]{ ...

  3. 用js获取当前月份的天数

    在获取每月天数的时候,一般都是存储到一个数组中进行获取,但是如果是二月份的话就需要首先判断是否闰年,再确定是28还是29了. js可以通过Date对象很方便的获取到每月的天数,在初始化Date对象时, ...

  4. JavaSE (二)

    this关键字 当一个对象创建后,Java虚拟机(JVM)就会给这个对象分配一个引用自身的指针,这个指针的名字就是 this. 用法:对当前对象的默认引用 调用自己的的构造方法. 用在构造方法内部,区 ...

  5. HTML meta标签总结,HTML5 head meta属性整理

    原文链接:http://caibaojian.com/mobile-meta.html <!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 -- ...

  6. VMWare Workstation使用总结几则[转]

    VMWare Workstation使用总结几则   1.安装 使用GHOST盘安装时一定要注意,需要把空盘建立分区并设置为主分区 PQ的使用形式,进入PQ找到磁盘设置为启用 否则 启动后显示Boot ...

  7. 【node】fs模块,文件和目录的操作

    检查文件是否存在,查询文件信息 fs.stat() fs.stat('./server.js', function (err, stat) { if (stat && stat.isF ...

  8. Android 开发中三种多线程

    在开发工程中线程可以帮助我们提高运行速度,Android开发中我知道的线程有四个一个是老生长谈的Thread,第二个是asyncTask,第三个:TimetTask,第四个是Looper,四个多线程各 ...

  9. 回归JavaScript基础(六)

    主题:引用类型Date.RegExp的介绍. 上节主要主要介绍了Object.Array引用类型.这节将继续为大家介绍引用类型,并对书中的一些知识点进行总结与归纳,也借此巩固自己对JavaScript ...

  10. zTree自定义节点name显示样式

    //setting中配置属性 view: { showLine:false, dblClickExpand: true, showIcon: false, addDiyDom: addDiyDom / ...