【JAVA大数训练】How Many Fibs?

How Many Fibs?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3628    Accepted Submission(s): 1457

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

 

Source

University of Ulm Local Contest 2000

 

import java.math.BigInteger;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		BigInteger [] p=new BigInteger[10010];
        p[1]=BigInteger.valueOf(1);p[2]=BigInteger.valueOf(2);
		for (int i = 3; i<=10000; i++) {
			p[i]=p[i-1].add(p[i-2]);
		}
		while(input.hasNext())
		{
			BigInteger n=input.nextBigInteger();
			BigInteger m=input.nextBigInteger();
			int sum=0;
			if(n.compareTo(BigInteger.ZERO)==0&&m.compareTo(BigInteger.ZERO)==0)
			break;
			for (int i = 1; i <=10000; i++) {
				if(p[i].compareTo(n)>=0&&p[i].compareTo(m)<=0)
					sum++;
			}
		    System.out.println(sum);
		}
	}
}

 

总结

BigIngter a与BigIngter b比大小

如果a比b大

a.compareTo(b)>0

如果a比b小

a.compareTo(b)<0

如果a等于b

a.compareTo(b)==0

 

时间: 2024-09-11 04:06:39

【JAVA大数训练】How Many Fibs?的相关文章

【JAVA大数训练】大明A+B

大明A+B Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7440    Accepted Submission(s): 2622 Problem Description 话说,经过了漫长的一个多月,小明已经成长了许多,所以他改了一个名字叫"大明". 这时他已经不是那个只会做100以内加法的那个"小明"了

【JAVA大数训练】A + B Problem II

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 196702    Accepted Submission(s): 37626 Problem Description I have a very simple problem for you. Given two integers A and B, yo

【JAVA大数训练】Exponentiation

Exponentiation Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6571    Accepted Submission(s): 1824 Problem Description Problems involving the computation of exact values of very large magnitude

【JAVA大数训练】N!

N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 50539    Accepted Submission(s): 14212 Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!   Input One N in o

【JAVA大数训练】大菲波数

大菲波数 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10291    Accepted Submission(s): 3461 Problem Description Fibonacci数列,定义如下: f(1)=f(2)=1 f(n)=f(n-1)+f(n-2) n>=3. 计算第n项Fibonacci数值.   Input 输

java大数乘法的简单实现 浮点数乘法运算_java

复制代码 代码如下: import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.regex.Matcher;import java.util.regex.Pattern; /** * 大数乘法的简单实现, 目前还不是很完善 * Fix:  * 1. 修改前后删除0的一些错误情况 * 2. 支持负数运算 * 3. 判断输入字符串是否符合小数定义

java大数相乘,希望出界抛异常。

问题描述 如题,我自己测试的时候,如果两个int型大数相乘,结果为负,求检测溢出的方法,并掷出异常的代码 解决方案 /** * Multiply two integers, checking for overflow. * * @param x a factor * @param y a factor * @return the product <code>x*y</code> * @throws ArithmeticException if the result can not

介绍Java的大数类(BigDecimal)和八种舍入模式_java

1.BigDecimal简介 BigDecimal 由任意精度的整数非标度值 和32 位的整数标度 (scale) 组成.如果为零或正数,则标度是小数点后的位数.如果为负数,则将该数的非标度值乘以 10 的负scale 次幂.因此,BigDecimal表示的数值是(unscaledValue × 10-scale). 2.BigDecimal的引入 在利用Java编程语言开发银行.金融类等需要对数值进行高精度计算的软件时,我们经常使用BigDecimal和BigInteger这两个大数类,而不是

HDOJ 1042 N!(大数阶乘JAVA)

Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N in one line, process to the end of file. Output For each N, output N! in one line. Sample Input 1 2 3 Sample Output 1 2 6 JAVA 大数轻松AC! import java.math.Bi