HDU 1316 How Many Fibs?(java,简单题,大数)

题目

/**
* compareTo:根据该数值是小于、等于、或大于 val 返回 -1、0 或 1;
public int
compareTo(BigInteger val)
将此 BigInteger 与指定的 BigInteger
进行比较。
对于针对六个布尔比较运算符 (<, ==, >, >=, !=,
<=)
中的每一个运算符的各个方法,优先提供此方法。
执行这些比较的建议语句是:(x.compareTo(y) <op>
0),
其中 <op> 是六个比较运算符之一。
*/

/**
* and:等同于c++的&&,且;
*/

import java.io.*;
import java.util.*;
import java.math.*;

public class Main {

/**
* @xqq
*/
public int an(BigInteger a, BigInteger b, BigInteger sa, BigInteger sb) {
int ans = 0;
if(a.compareTo(sa) >= 0 && a.compareTo(sb) <= 0) {
ans++;
}
if(b.compareTo(sa) >= 0 && b.compareTo(sb) <= 0) {
ans++;
}
for(;;) {
BigInteger c = a.add(b);
a = b;
b = c;
if(b.compareTo(sa) >= 0 && b.compareTo(sb) <= 0) {
ans++;
}
if(b.compareTo(sb) > 0) {
return ans;
}
}

}
public static void main(String[] args) throws Exception {
// 定义并打开输入文件
Scanner cin = new Scanner(System.in);

Main e = new Main();
BigInteger a = BigInteger.valueOf(1);
BigInteger b = BigInteger.valueOf(2);
BigInteger sa;
BigInteger sb;
BigInteger zero = BigInteger.ZERO;

while(cin.hasNext()) {
sa = cin.nextBigInteger();
sb = cin.nextBigInteger();
if(sa.compareTo(zero) == 0 && sb.compareTo(zero) == 0) {
break;
}
System.out.println(e.an(a, b, sa, sb));
}

cin.close(); //关闭输入文件
}
}

HDU 1316 How Many Fibs?(java,简单题,大数),布布扣,bubuko.com

时间: 2024-08-02 15:11:26

HDU 1316 How Many Fibs?(java,简单题,大数)的相关文章

HDU 1715 大菲波数(JAVA, 简单题,大数)

题目 //BigInteger 和 BigDecimal 是在java.math包中已有的类,前者表示整数,后者表示浮点数 import java.io.*; import java.util.*; import java.math.*; public class Main { /** * @xqq */ public BigInteger an(BigInteger a, BigInteger b, int n) { if(n == 1) { return a; } for(int i = 2

HDU 1316 How Many Fibs? java大数(水

水一水.. import java.math.*; import java.util.*; import java.io.*; public class Main { BigInteger[] fib = new BigInteger[505]; public void work(){ fib[1] = BigInteger.ONE; fib[2] = BigInteger.valueOf(2); for(int i = 3; i <= 500; i++) fib[i] = fib[i-1].a

hdu 1201 18岁生日 (简单题)

18岁生日 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18281    Accepted Submission(s): 5776 Problem Description Gardon的18岁生日就要到了,他当然很开心,可是他突然想到一个问题,是不是每个人从出生开始,到达18岁生日时所经过的天数都是一样的呢?似乎并不全都是这样,所以他

hdu 1202The calculation of GPA (简单题+坑)

The calculation of GPA Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18748    Accepted Submission(s): 4331 Problem Description 每学期的期末,大家都会忙于计算自己的平均成绩,这个成绩对于评奖学金是直接有关的.国外大学都是计算GPA(grade point a

hdu 1316 How Many Fibs? (模拟高精度)

题目大意: 问[s,e]之间有多少个 斐波那契数. 思路分析: 直接模拟高精度字符串的加法和大小的比较. 注意wa点再 s 可以从 0 开始 那么要在判断输入结束的时候注意一下. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; struct node { char str[111]; int len; void

hdu 1316 How many Fibs?(高精度斐波那契数)

//  大数继续 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 cas

hdu 1253 胜利大逃亡(简单题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1253 题目大意:在所给的时间能顺利离开城堡. 1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 #include <cstring> 5 using namespace std; 6 int map[55][55][55],visit[55][55][55],a,b,c,T; 7 int

zoj Fibonacci Numbers ( java , 简单 ,大数)

题目 //f(1) = 1, f(2) = 1, f(n > 2) = f(n - 1) + f(n - 2) import java.io.*; import java.util.*; import java.math.*; public class Main { /** * @xqq */ public BigInteger an(int n) { BigInteger c; BigInteger a = BigInteger.valueOf(1); BigInteger b = BigIn

POJ 3982 序列(JAVA,简单,大数)

题目 //在主类中 main 方法必须是 public static void 的,在 main 中调用非static类时会有警告信息, //可以先建立对象,然后通过对象调用方法: import java.io.*; import java.util.*; import java.math.*; public class Main { /** * @xqq */ public BigInteger a99(BigInteger a, BigInteger b, BigInteger c) { f