UVA - 10183 - How Many Fibs? (斐波那契 + 高精度)
來源:程序員人生 發布時間:2015-08-07 08:07:42 閱讀次數:2935次
題目傳送: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);
}
}
}
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈