(compareTo) How Many Fibs hdu1316 && ZOJ1962
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的更多相关文章
- How many Fibs?【sudt 2321】【大数的加法及其比较】
How many Fibs? Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Recall the definition of t ...
- 【HDOJ】1316 How Many Fibs?
Java水了. import java.util.Scanner; import java.math.BigInteger; public class Main { public static voi ...
- HDOJ 1316 How Many Fibs?
JAVA大数.... How Many Fibs? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- hdu--1316--How Many Fibs?(java大数)
How Many Fibs? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- java 中时间的比较 用compareTo方法
//compareTo 方法 是对象比较 大于 1 等于 返0 小于 返 -1 列 Date dat1=new Date(); Date dat2=new Date(); int va ...
- 通过实现System.IComparable接口的CompareTo方法对两个类进行比较
假设现在有一个学生类 class Student { int age; public Student(int age) { this.age = age; } } 要使学生类之间能进行比较,实现Sys ...
- C# IComparable接口、IComparer接口和CompareTo(Object x)方法、Compare()方法
在项目中经常会用到字符串比较,但是有时候对字符串的操作比较多,规则各异.比如有的地方我们需要用排序规则,有的地方需要忽略大小写,我们该如何写一个比较容易操作的比较方法呢?重新实现IComparer接口 ...
- C# CompareTo 和 String.Compare
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- String类的compareTo()方法的源码解析
private final char value[]; 字符串会自动转换为一个字符数组. public int compareTo(String anotherString) { //this -- ...
随机推荐
- Week 2 代码规范
Question 1: 这些规范都是官僚制度下产生的浪费大家的编程时间.影响人们开发效率, 浪费时间的东西. My opinion: 我认为恰恰相反,这个可以提高人们的开发效率. 在团队合作当中,如果 ...
- linux内核分析ELF文件分析实践报告
- 2017-2018-2 1723《程序设计与数据结构》实验四 & 实验五 & 课程总结 总结
作业地址 实验四作业:https://edu.cnblogs.com/campus/besti/CS-IMIS-1723/homework/1943 提交情况如图: 实验五作业:https://edu ...
- 实训十二(stick的设定)
上篇我们介绍到人物主角的设定,其实人物是有工具使的,那就是——stick小棍. 信息的获取.起始位置.长度的加载.边界的判断.位置.长度重置是需要我们主要考虑的问题 信息获取上考虑的使什么时候加载st ...
- <<架构漫谈>>读后感
今天按照老师的要求,看了架构漫谈1--9讲,觉得受益良多,以下是我得点点读后感: (一)什么是架构? 架构的英文是Architecture,从定义上看,架构好像是一个过程,也不是很清晰.下面从架构的缘 ...
- java — 静态绑定和动态绑定
绑定:一个方法的调用与方法所在的类关联起来.java中的绑定分为静态绑定和动态绑定,又被称作前期绑定和后期绑定. 静态绑定:(final.static.private)在程序执行前已经被绑定,也就是说 ...
- Caffe2的安装
源码下载 首先下载caffe2的源码:https://github.com/caffe2/caffe2 网上都建议使用git命令下载,因为caffe2依赖了很多第三方模块,git会根据依赖自动下载第三 ...
- Linux MYSQL:dead but pid file exists
MYSQL dead but pid file exists问题 - CSDN博客https://blog.csdn.net/shilian_h/article/details/38020567 Er ...
- JavaScript全局变量的本质及页面共享问题
https://zhidao.baidu.com/question/1668225805834130107.html 全局变量默认为window的成员,window即代表浏览器窗口 全局变量均为win ...
- apache Storm学习之二-基本概念介绍
2.1 Storm基本概念 在运行一个Storm任务之前,需要了解一些概念: Topologies Streams Spouts Bolts Stream groupings Reliability ...