zoukankan      html  css  js  c++  java
  • HDU1130_How Many Trees?(卡特兰数 _java)

    题目大意:                   就是给你1到N个数,让你求他能构成多少种二叉树; 题目分析:                    这里又是一种组合数学里的重要知识点!Catalan数的应用。                    Catelan有直接的公式,用java的BigInteger类就可以迅速搞掉了,用c/c++要慢慢模拟,不过速度会更快~各有特色吧。Java用了140MS                    //令h(1)=1,h(0)=1,catalan数满足递归式:                   //h(n)=(4*n-2)/(n+1)*h(n-1);
     
    import java.math.BigInteger;
    import java.util.Scanner;
    public class Main
    {
    	public static void main(String []args)
    	{
    		Scanner cin=new Scanner(System.in);
    		BigInteger h[]=new BigInteger[1005];
    		h[0]=new BigInteger("1");
    		h[1]=new BigInteger("1");
    		BigInteger one=new BigInteger("1");
    		BigInteger four=new BigInteger("4");
    		BigInteger two=new BigInteger("2");
    		for(int i=2;i<1005;i++)
    		{
    			String str=String.valueOf(i);
    			BigInteger n=new BigInteger(str);
    			BigInteger temp=new BigInteger("0");
    			h[i]=(n.multiply(four).subtract(two)).multiply(h[i-1]).divide(n.add(one));
    			//h[i]=temp.divide(n.add(one));
    			//h[i]=h[i-1].multiply(4*n-1).divide(n.add());	
    		}
    		while(cin.hasNext())
    		{
    			//令h(1)=1,h(0)=1,catalan数满足递归式: 
    			//h(n)=(4*n-2)/(n+1)*h(n-1); 
    			int n=cin.nextInt();
    			System.out.println(h[n]);
    		}
    	}
    }
  • 相关阅读:
    计算机问题:VirtualBox启动崩溃问题
    软件工程:代码静态分析工具
    oracle 游标
    oracle 控制语句
    oracle 数据类型及函数
    oracle 表空间Tablespaces
    java.lang.RuntimeException: java.sql.SQLSyntaxErrorException: ORA-00911: 无效字符
    oracle 用户解锁和修改用户密码
    shiro + maven 的web配置(不整合spring)
    配置shiro错误
  • 原文地址:https://www.cnblogs.com/cchun/p/2520155.html
Copyright © 2011-2022 走看看