当前位置: 代码迷 >> 综合 >> Project Euler problem 57
  详细解决方案

Project Euler problem 57

热度:34   发布时间:2024-01-13 17:35:15.0


观察这个分子序列  设分子为b序列 ,分母为a 序列

则他们满足一下条件

a[i] = a[i - 1] * 2 + a[i - 2]

b[i] = b[i - 1] * 2+b[i - 2]

然后就发现需要高精度


import java.math.*;
import java.util.*;public class Main {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubBigInteger a[] = new BigInteger [1111];BigInteger b[] = new BigInteger [1111];a[0] = BigInteger.valueOf(1);b[0] = BigInteger.valueOf(1);a[1] = BigInteger.valueOf(2);b[1] = BigInteger.valueOf(3);for(int i = 2; i <= 1000; i++){a[i] = a[i - 1].multiply(BigInteger.valueOf(2)).add(a[i - 2]);b[i] = b[i - 1].multiply(BigInteger.valueOf(2)).add(b[i - 2]);}int ans = 0;for(int i = 2; i <= 1000; i++){if(b[i].toString().length() > a[i].toString().length()) ans++;}System.out.println(ans);}}


  相关解决方案