Java大数统计

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

题目描述:

         给你一个范围,问你在这个范围内有多少斐波拉契数。

代码实现:

1 1 import java.util.Scanner; 2 2 import java.math.BigInteger; 3 3 public class Main{ 4 4 5 5 @SuppressWarnings("null") 6 6 public static void main(String[] args) { 7 7 Scanner cin=new Scanner(System.in); 8 8 int sum=0; 9 9 BigInteger []big=new BigInteger[1005];//定义一个BigInteger类型的数组 1010 big[1]=BigInteger.valueOf(1); 1111 big[2]=BigInteger.valueOf(2); 1212 //先将前1005个斐波拉契数求出来放在big数组里面 1313 for(int i=3;i<1005;i++){ 1414 big[i]=big[i-1].add(big[i-2]);//大数相加,第i个数=第i-1个数+第i-2个数 1515 } 1616 while(cin.hasNext()) 1717 { 1818 BigInteger a = cin.nextBigInteger(); 1919 BigInteger b = cin.nextBigInteger(); 2020 //如果a=0&&b=0,输入结束 2121 if(a.compareTo(BigInteger.valueOf(0))==0&&b.compareTo(BigInteger.valueOf(0))==0) return; 2222 for(int i=1;i<1005;i++){ 2323 //如果big[i]这个数处在[a,b]范围内,sum+1 2424 if(big[i].compareTo(a)>=0&&big[i].compareTo(b)<=0){ 2525 sum++; 2626 } 2727 } 2828 System.out.println(sum); 2929 sum=0; 3030 } 3131 cin.close(); 3232 } 3333 }
点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

hdu2204 Eddy's爱好

原题:点击打开链接(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Facm.hdu.edu.cn%2Fshowproblem.php%3Fpid%3D2204)题意很明确了,即:给你一个正整数N,确定在1到N之间有多少个可以表示成M^K(K1)的数。本题目为容斥原

2019 HDOJ Multi

服务器时不时爆炸,有点难受。题目链接:http://acm.hdu.edu.cn/userloginex.php?cid849(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Facm.hdu.edu.cn%2Fuserloginex.php%3Fcid%3D849)

P2P技术揭秘.P2P网络技术原理与典型系统开发

Modular.Java(2009.06)\.Craig.Walls.文字版.pdf:http://www.t00y.com/file/59501950(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.t00y.com%2Ffile%2F59501950)\More.E

Codeforces Round #479 (Div. 3) F. Consecutive Subsequence

标签:DP题目链接(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fcodeforces.com%2Fcontest%2F977%2Fproblem%2FF)

1418:猴子选大王

题目连接(本题目链接失效内容同《围圈报数》):http://ybt.ssoier.cn:8088/problem\_show.php?pid1418(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fybt.ssoier.cn%3A8088%2Fproblem_show.php%3Fpid

Java大数统计 - HelloWorld