hdu1316 大整数加法(比较大小)

求大整数区间内斐波那契数:

 1 import java.io.*;
 2 import java.util.*;
 3 import java.math.BigInteger;
 4 public class Main {
 5     public static void main(String[] args)
 6     {
 7         Scanner in=new Scanner(System.in);
 8         BigInteger f[]=new BigInteger[505];
 9         BigInteger zz=new BigInteger("0");
10         f[1]=new BigInteger("1");
11         f[2]=new BigInteger("2");
12         for (int i=3;i<=500;i++)
13             f[i]=f[i-1].add(f[i-2]);
14         while (in.hasNext())
15         {
16             BigInteger a=in.nextBigInteger();
17             BigInteger b=in.nextBigInteger();
18             if  ( a.compareTo(zz)==0 && b.compareTo(zz)==0 ) break;
19             int ans=0;
20             for (int i=1;i<=500;i++)
21             {
22                 if (a.compareTo(f[i])<=0 && b.compareTo(f[i])>=0) ans++;
23                 if (b.compareTo(zz)<0) break;
24             }
25             System.out.println(ans);
26         }
27     }
28 }

compare函数;

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1316

posted on 2014-12-30 02:26  xiao_xin  阅读(171)  评论(0编辑  收藏  举报

导航