How Many Fibs?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 7007    Accepted Submission(s): 2761

Problem Description

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

用Java,然后利用compareTo来判断大小。

import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
BigInteger a,b;
while(in.hasNextBigInteger()) {
a=in.nextBigInteger();
b=in.nextBigInteger();
if(a.equals(BigInteger.valueOf(0))&&b.equals(BigInteger.valueOf(0)))
break; //也可以用compareTo。
System.out.println(Fib(a,b));
}
}
public static int Fib(BigInteger a,BigInteger b)
{
int sum = 0;
BigInteger f = new BigInteger("1");
BigInteger s = new BigInteger("2");
BigInteger tmp;
while(true)
{
if(f.compareTo(b)>0)
break;
if(f.compareTo(a) >= 0)
sum++;
tmp = f; //可以用迭代的方法求斐波那契数列。
f = s;
s = s.add(tmp);
}
return sum;
}
}

(compareTo) How Many Fibs hdu1316 && ZOJ1962的更多相关文章

  1. How many Fibs?【sudt 2321】【大数的加法及其比较】

    How many Fibs? Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Recall the definition of t ...

  2. 【HDOJ】1316 How Many Fibs?

    Java水了. import java.util.Scanner; import java.math.BigInteger; public class Main { public static voi ...

  3. HDOJ 1316 How Many Fibs?

    JAVA大数.... How Many Fibs? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  4. hdu--1316--How Many Fibs?(java大数)

    How Many Fibs? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  5. java 中时间的比较 用compareTo方法

    //compareTo 方法  是对象比较 大于 1   等于  返0  小于  返 -1 列 Date  dat1=new Date(); Date  dat2=new Date(); int va ...

  6. 通过实现System.IComparable接口的CompareTo方法对两个类进行比较

    假设现在有一个学生类 class Student { int age; public Student(int age) { this.age = age; } } 要使学生类之间能进行比较,实现Sys ...

  7. C# IComparable接口、IComparer接口和CompareTo(Object x)方法、Compare()方法

    在项目中经常会用到字符串比较,但是有时候对字符串的操作比较多,规则各异.比如有的地方我们需要用排序规则,有的地方需要忽略大小写,我们该如何写一个比较容易操作的比较方法呢?重新实现IComparer接口 ...

  8. C# CompareTo 和 String.Compare

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. String类的compareTo()方法的源码解析

    private final char value[]; 字符串会自动转换为一个字符数组. public int compareTo(String anotherString) { //this -- ...

随机推荐

  1. 归并排序O(nlogn)

    先分治再合并 代码 #include<bits/stdc++.h> using namespace std; #define ll long long int a[1000],t[1000 ...

  2. Scrum Meeting NO.6

    Scrum Meeting No.6 1.会议内容 今晚是提交编译测试程序的截至日期,大家果断都在忙着写编译,所以今天的进度不大. 2.任务清单 徐越 序号 近期的任务 进行中 已完成 1 代码重构: ...

  3. ros-安装

    1.安装了ubuntu for ros. 运行评论下边那条命令: 2.rtabamp 3.准备安装机器人导航仿真系统:https://blog.csdn.net/wangchao7281/articl ...

  4. atcoder A - Frog 1(DP)

    A - Frog 1 Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement There a ...

  5. Flask-论坛开发-5-memcached缓存系统

    对Flask感兴趣的,可以看下这个视频教程:http://study.163.com/course/courseLearn.htm?courseId=1004091002 ### 介绍:哪些情况下适合 ...

  6. Maven修改默认JDK

    Maven修改默认JDK 问题: 1.创建maven项目的时候,jdk版本是1.5版本,而自己安装的是1.7或者1.8版本. 2.每次右键项目名-maven->update project 时候 ...

  7. HTML5 Base64_encoding_and_decoding

    https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding In JavaSc ...

  8. Centos7安装Splash

    前言 最近在用Scrapy抓取一个网站数据,其中有个页面需要执行js脚本才能加载,所以需要用到Splash. 官网地址:https://splash.readthedocs.io/en/stable/ ...

  9. loadrunner基础学习笔记七-面向目标场景

    部署应用程序之前,要执行验收测试以确保系统能够承担预期的实际工作量. 可以为想要生成的每秒点击次数,每秒事务数或事务响应时间设置目标 loadrunner将使用面向目标的场景自动生成所需的目标,当应用 ...

  10. 一本通1546【NOIP2011】选择客栈

    1546:NOIP2011 选择客栈 时间限制: 1000 ms         内存限制: 524288 KB 题目描述 丽江河边有 n 家很有特色的客栈,客栈按照其位置顺序从 1 到 n 编号. ...