题目地址:http://poj.org/problem?id=1405
打表找规律:
import java.util.*;
import java.math.*;
import java.text.*;
import java.io.*;public class Main
{public static void main(String[] args) {Scanner cin = new Scanner(new BufferedInputStream(System.in));BigDecimal now = BigDecimal.ONE;BigDecimal temp= BigDecimal.ZERO;for(int i=2;i<=10000000;i++){BigDecimal v = BigDecimal.ONE;temp=now.subtract(v.divide(BigDecimal.valueOf(i),100,BigDecimal.ROUND_HALF_EVEN));if(temp.compareTo(BigDecimal.valueOf(0))==1) {System.out.println(i);now=now.subtract(BigDecimal.ONE.divide(BigDecimal.valueOf(i),100,BigDecimal.ROUND_HALF_EVEN));}}}
}
AC code:
import java.util.*;
import java.math.*;
import java.text.*;
import java.io.*;public class Main
{public static void main(String[] args) {Scanner cin = new Scanner(new BufferedInputStream(System.in));BigInteger[] ans = new BigInteger[20];ans[1]=BigInteger.valueOf(2);for (int i=2; i<=18; ++i) {ans[i]=ans[i-1].pow(2).subtract(ans[i-1]).add(BigInteger.ONE);}while(cin.hasNext()){int n=cin.nextInt();for (int i=1; i<=n; ++i) {System.out.println(ans[i]);}}}
}