• 杭电_ACM_Count the Trees


    Problem Description
    Another common social inability is known as ACM (Abnormally Compulsive Meditation). This psychological disorder is somewhat common among programmers. It can be described as the temporary (although frequent) loss of the faculty of speech when the whole power of the brain is applied to something extremely interesting or challenging. 
    Juan is a very gifted programmer, and has a severe case of ACM (he even participated in an ACM world championship a few months ago). Lately, his loved ones are worried about him, because he has found a new exciting problem to exercise his intellectual powers, and he has been speechless for several weeks now. The problem is the determination of the number of different labeled binary trees that can be built using exactly n different elements. 

    For example, given one element A, just one binary tree can be formed (using A as the root of the tree). With two elements, A and B, four different binary trees can be created, as shown in the figure. 

    If you are able to provide a solution for this problem, Juan will be able to talk again, and his friends and family will be forever grateful. 

     
    Input
    The input will consist of several input cases, one per line. Each input case will be specified by the number n ( 1 ≤ n ≤ 100 ) of different elements that must be used to form the trees. A number 0 will mark the end of input and is not to be processed. 
     
    Output
    For each input case print the number of binary trees that can be built using the n elements, followed by a newline character. 
     
    Sample Input
    1
    2
    10
    25
    0
     
    Sample Output
    1
    4
    60949324800
    75414671852339208296275849248768000000
    View Code
     1 #include <stdio.h>
     2 int a[200];
     3 int main()
     4 {
     5     int length, carry, n, i, j;
     6     while (scanf("%d", &n) != EOF)
     7     {
     8         if (!n)
     9             break;
    10         if (n == 1)
    11         {
    12             puts("1");
    13             continue;
    14         }
    15         length = 1;
    16         a[1] = 1;
    17         // (n + 2) * (n + 3)...(2 * n)
    18         for (i = n + 2; i <= 2 * n; i++)
    19         {
    20             carry = 0;
    21             for (j = 1; j <= length; j++)
    22             {
    23                 carry += a[j] * i;
    24                 a[j] = carry % 10;
    25                 carry /= 10;
    26             }
    27             while (carry)
    28             {
    29                 a[++length] = carry % 10;
    30                 carry /= 10;
    31             }
    32         }
    33         //formatting printing
    34         for (i = length; i >= 1; i--)
    35         {
    36             printf("%d", a[i]);
    37         }
    38         puts("");
    39     }
    40     return 0;
    41 }

    Key points

    firstly, you should be familar with catalan, particular the regular.

    secondly, for getting result in advance and get result according to input, it all ok for the question. In my opinion, if the result depends to the previous, then getting result in advance is more beneift.

  • 相关阅读:
    dB是乘以10还是乘以20
    FFT快速傅里叶变换的python实现
    f(t) = t的傅里叶系数
    如何从二进制文件中读取int型序列
    python中的bytes和str类型
    python文件读写
    matplotlib浅析
    什么是语法糖
    怎样查看一个可迭代对象的值
    十六进制颜色码及其表示-(6 digit color code)
  • 原文地址:https://www.cnblogs.com/chuanlong/p/2758381.html
Copyright © 2020-2023  润新知