(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 -- ...
随机推荐
- JS 实现计算一段文字中的字节数,字母数,数字数,行数,汉字数。
看到了匹配,第一个想到了用正则表达式,哈哈,果然很方便.不过正则表达式高深莫测!我还没有研究明白啊..目前学了点皮毛.代码如下: <!DOCTYPE html PUBLIC "-//W ...
- M1m2分析报告
个人博客链接: http://www.cnblogs.com/kjzxzzh/p/4074386.html http://www.cnblogs.com/kjzxzzh/p/4027699.html ...
- WebAPI实例--第一个API
今天终于做了第一个任务,学习API之后的第一个实例.销售设置开发API. 第一.层次结构 1.API各层 项目结构主要有五层,分别为API.BizModel.Data.DBModel.Logic. 2 ...
- 最新一课 老师指点用Listview适配器
上课前 <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android ...
- 第六次作业-my Backlog
杨灵超小组 My Backlog 小学生四则运算自动生成(Backlog) ID Name IMP EST How to Demo ...
- ADC转换的分辨率
分辨率是指ADC能够分辨量化的最小信号的能力.分辨率用二进制位数表示.例如对一个10位的ADC,其所能分辨的最小量化电平为参考电平(满量程)的2的10次方分之一.也就是说分辨率越高,就能把满量程里的电 ...
- jQuery(五)
wrap <script> //wrap:包装 //wrapAll:整体包装 //wrapInner:内部包装 //unwrap:删除包装(删除父级,不包括body) $(function ...
- id生成工具类
import java.util.Random; /** * 各种id生成策略 * <p>Title: IDUtils</p> * <p>Description: ...
- PAT 1041 考试座位号
https://pintia.cn/problem-sets/994805260223102976/problems/994805281567916032 每个PAT考生在参加考试时都会被分配两个座位 ...
- NF5280M4 安装 Win2016 的方法
1. 前提条件, 硬盘大于2T, 2. 必须使用最新版本的 Win2016 首先 win2016的可用序列号 • Windows Server 数据中心 CB7KF-BWN84-R7R2Y-793K2 ...