zoukankan      html  css  js  c++  java
  • 卡特兰数

    题目链接:http://poj.org/problem?id=2084

    Game of Connections
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions:9467   Accepted: 4639

    Description

    This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, . . . , 2n - 1, 2n consecutively in clockwise order on the ground to form a circle, and then, to draw some straight line segments to connect them into number pairs. Every number must be connected to exactly one another. 
    And, no two segments are allowed to intersect. 
    It's still a simple game, isn't it? But after you've written down the 2n numbers, can you tell me in how many different ways can you connect the numbers into pairs? Life is harder, right?

    Input

    Each line of the input file will be a single positive number n, except the last line, which is a number -1. 
    You may assume that 1 <= n <= 100.

    Output

    For each n, print in a single line the number of ways to connect the 2n numbers into pairs.
     
    思路:卡特兰数列,f[0] = 1, f[1] = 1, f[n] = f[n - 1] * (4 * n - 2) / (n + 1);大数 用java打表写即可。
     1 import java.util.*;
     2 import java.math.BigInteger;
     3 
     4 public class Main
     5 {
     6     public static void main(String [] args)
     7     {
     8         BigInteger [] a = new BigInteger [110];
     9         a[0] = BigInteger.ONE;
    10         a[1] = BigInteger.valueOf(1);
    11         for(int i = 2; i <= 100; i ++)
    12         {
    13             int x = 4 * i - 2, y = i + 1;
    14             a[i] = a[i - 1].multiply(BigInteger.valueOf(x)).divide(BigInteger.valueOf(y));
    15         }
    16         Scanner cin = new Scanner (System.in);
    17         int n;
    18         while(true)
    19         {
    20             n = cin.nextInt();
    21             if(n == -1)
    22                 break;
    23             System.out.println(a[n]);
    24         }
    25     }
    26 }
    View Code
  • 相关阅读:
    nginx缓存
    java 常用命令工具
    shell流程控制--循环语句
    Python 文档 涉及词汇
    python 学习网站
    python web入门程序
    编译安装 Python3.6.1
    正则表达式与grep
    编译安装 Nginx1.12.1
    查看进程号
  • 原文地址:https://www.cnblogs.com/yuanweidao/p/11256224.html
Copyright © 2011-2022 走看看