当前位置: 代码迷 >> 综合 >> POJ 1405 Heritage 找规律+JAVA .
  详细解决方案

POJ 1405 Heritage 找规律+JAVA .

热度:103   发布时间:2023-09-23 04:25:10.0

题目地址: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));}}}
}



可得A(n)=A(n-1)^2 - A(n-1) +1;

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]);}}}
}




  相关解决方案