How many Fibs?
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
Recall the definition of the Fibonacci numbers:
f1 := 1Given two numbers a and b, calculate how many Fibonacci numbers are in the range [a,b].
f2 := 2
fn := fn-1 + fn-2 (n>=3)
输入
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++语言做起来简单~
1 import java.io.*; 2 import java.math.*; 3 import java.util.*; 4 public class Main 5 { 6 public static void main(String args[]) 7 { 8 Scanner scn=new Scanner(System.in); 9 while(true) 10 { 11 BigInteger a=scn.nextBigInteger(); 12 BigInteger b=scn.nextBigInteger(); 13 if(a.equals(new BigInteger("0"))&&b.equals(new BigInteger("0"))) 14 { 15 break; 16 } 17 BigInteger f[]=new BigInteger[20000]; 18 f[1]=new BigInteger("1"); 19 f[2]=new BigInteger("2"); 20 int i; 21 for(i=3;i<=600;i++) 22 { 23 int temp=i; 24 f[i]=f[temp-1].add(f[temp-2]); 25 } 26 int count=0; 27 for(i=1;i<=600;i++) 28 { 29 if(f[i].compareTo(a)==0) 30 { 31 count=1; 32 } 33 else if(f[i].compareTo(a)>0&&f[i].compareTo(b)<=0) 34 { 35 count++; 36 } 37 else if(f[i].compareTo(b)>0) 38 { 39 break; 40 } 41 } 42 System.out.println(count); 43 } 44 } 45 }