HDU-1316 How Many Fibs?(Java大数)

来源:互联网 发布:windows寨板 哪款好 编辑:程序博客网 时间:2024/05/16 06:38

Here

给定一个区间求斐波那契数的个数,利用循环递推即可

import java.math.BigInteger;import java.util.Scanner;public class Main {    public static void main(String [] args){        Scanner in = new Scanner(System.in);        BigInteger a,b;        BigInteger ZERO = BigInteger.ZERO;        while(in.hasNext())        {            a = in.nextBigInteger();            b = in.nextBigInteger();            if(a.compareTo(ZERO)==0 && b.compareTo(ZERO)==0)                break;            System.out.println(Fib(a,b));        }    }    //第一次写函数啊    public static int Fib(BigInteger a,BigInteger b)    {        int sum = 0;        BigInteger f = new BigInteger("1");        BigInteger s = new BigInteger("2");        BigInteger tmp;        while(true)        {            if(f.compareTo(b)>0)                break;            if(f.compareTo(a) >= 0)                sum++;            tmp = f;            f = s;            s = s.add(tmp);        }        return sum;    }    }


0 0
原创粉丝点击