zoukankan      html  css  js  c++  java
  • java高精度hdu4043

    可以推出公式

    f[n]=f[n-1]+f[n-1]*2*(n-1)

    f[1]=1;

    数据量很大,最后又要进行gcd操作,java里竟然自带了一个gcd的函数,为了避免求大数取余和大数除法操作,还是用java比较快,而且这题对时间复杂度要求不是很高。

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    import java.math.BigInteger;
    import java.util.Scanner;
    
    /**
     *
     * @author chen
     */
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            Scanner cin=new Scanner(System.in);
            //System.out.println("How many number do you need to draw"); 
            int T;
            T=cin.nextInt();
            while(T!=0)
            {
                T--;
                int n;
                n=cin.nextInt();
                BigInteger up= BigInteger.valueOf(1);
                BigInteger down= BigInteger.valueOf(2);
                BigInteger tmp= BigInteger.valueOf(1);
                for(int i=2;i<=n;i++)
                {
                    tmp=up;
                    BigInteger tmp1= BigInteger.valueOf( 2*(i-1) );
                    //System.out.println(tmp1);
                    tmp=tmp.multiply(tmp1);
                    up=up.add(tmp);
                    //System.out.println(up);
                    tmp1=BigInteger.valueOf(2*i);
                   // System.out.println(tmp1);
                    down=down.multiply(tmp1);
                    //System.out.println(down);
                }
                //System.out.println(up);
                //System.out.println(down);
                tmp=up.gcd(down);
                up=up.divide(tmp);
                down=down.divide(tmp);
                System.out.println(up+"/"+down);
            }
        }
    }

    还有就是Netbeans IDE 简洁又好用! 

  • 相关阅读:
    springAOP实现原理
    cglib用法
    git 用法
    java基础算法之快速排序
    记一次与a标签相遇的小事
    java设计模式之建造者模式
    HashMap源码分析
    Linux下安装nginx
    java设计模式之策略模式
    java设计模式之中介者模式
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/3200618.html
Copyright © 2011-2022 走看看