UVA - 10183 - How Many Fibs? (斐波那契 + 高精度)
来源:程序员人生 发布时间:2015-08-07 08:07:42 阅读次数:2773次
题目传送:UVA - 10183
思路:高精度就能够了,由于10^100之内的斐波那契数不多,根据公式来看,估计就500多,开个1000的数组足够啦,实现的话是用的java,注意这里的斐波那契是从1开始的,我1开始是从0开始的,wa了1下
AC代码:
import java.util.Scanner;
import java.math.BigInteger;
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
BigInteger a, b;
BigInteger[] fibo = new BigInteger[1005];
fibo[0] = new BigInteger("1");
fibo[1] = new BigInteger("2");
for(int i = 2; i < 1005; i ++) {
fibo[i] = fibo[i - 2].add(fibo[i - 1]);
}
while(true) {
a = cin.nextBigInteger();
b = cin.nextBigInteger();
if(a.compareTo(BigInteger.ZERO) == 0 && b.compareTo(BigInteger.ZERO) == 0) {
break;
}
int ans = 0;
for(int i = 0; i < 1005; i ++) {
if(fibo[i].compareTo(a) != ⑴ && fibo[i].compareTo(b) != 1) {
ans ++;
}
}
System.out.println(ans);
}
}
}
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠