How many Fibs?
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
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].
输入
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<=10100. The numbers aand b are given with no superfluous leading zeros.
输出
示例输入
10 100
1234567890 9876543210
0 0
示例输出
5
4
提示
来源
2000/2001 University of Ulm Local Contest
示例程序
题目大意:
输入两个整数,求这两个整数之间的斐波纳契数的个数,即求[a,b]之间斐波那契数的个数,输入以0 0结束(a,b两个数可以到10^100次幂)
代码:
很多大数的问题用java做的话一定比c语言或者c++语言做起来简单~
import java.io.*;
import java.math.*;
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner scn=new Scanner(System.in);
while(true)
{
BigInteger a=scn.nextBigInteger();
BigInteger b=scn.nextBigInteger();
if(a.equals(new BigInteger("0"))&&b.equals(new BigInteger("0")))
{
break;
}
BigInteger f[]=new BigInteger[20000];
f[1]=new BigInteger("1");
f[2]=new BigInteger("2");
int i;
for(i=3;i<=600;i++)
{
int temp=i;
f[i]=f[temp-1].add(f[temp-2]);
}
int count=0;
for(i=1;i<=600;i++)
{
if(f[i].compareTo(a)==0)
{
count=1;
}
else if(f[i].compareTo(a)>0&&f[i].compareTo(b)<=0)
{
count++;
}
else if(f[i].compareTo(b)>0)
{
break;
}
}
System.out.println(count);
}
}
}