How many Fibs?【sudt 2321】【大数的加法及其比较】

How many Fibs?

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

Recall the definition of the Fibonacci numbers:

f 1  := 1  
f 2  := 2  
f n  :=   f n-1  +   f n-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++语言做起来简单~
 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 } 
View Code

 

转载于:https://www.cnblogs.com/kuangdaoyizhimei/p/3584283.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值