hdu 1316 How Many Fibs?

http://acm.hdu.edu.cn/showproblem.php?pid=1316

How Many Fibs?

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


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
 

 

Recommend
Eddy
 
View Code
 1 import java.math.BigInteger;
 2 import java.util.Scanner;
 3 
 4 
 5 public class Main {
 6     public static void main(String[] args) {
 7         BigInteger f[]=new BigInteger[1010];
 8         int a[]=new int[1010];
 9         BigInteger aa,bb;
10         Scanner cinScanner=new Scanner(System.in);
11         f[1]=BigInteger.ONE;
12         f[2]=BigInteger.valueOf(2);
13         for(int i=3;i<1001;i++)
14             f[i]=f[i-1].add(f[i-2]);
15         int n;
16         while(cinScanner.hasNext())
17         {
18             aa=cinScanner.nextBigInteger();
19             bb=cinScanner.nextBigInteger();
20             if(aa.compareTo(BigInteger.ZERO)==0&&bb.compareTo(BigInteger.ZERO)==0)
21                 break;
22             if(aa.compareTo(BigInteger.ONE)==0&&bb.compareTo(BigInteger.ONE)==0)
23             {
24                 System.out.println("1");
25                 continue;
26             }
27             int j=1;
28             while(f[j].compareTo(aa)<0)
29             {
30                 j++;
31             }
32             //System.out.println(j);
33             int count=0;
34             while((f[j]).compareTo(bb)<=0)
35             {
36                 count++;
37                 j++;
38             }
39             System.out.println(count);
40                 
41             
42         }
43         
44         
45     }
46 
47 }

 

posted on 2012-11-02 20:30  仁者无敌8勇者无惧  阅读(251)  评论(0编辑  收藏  举报

导航