您的位置:首页 > 其它

杭电_ACM_Count the Trees

2012-11-07 10:55 483 查看
[align=left]Problem Description[/align]
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.
View Code

#include <stdio.h>
int a[200];
int main()
{
int length, carry, n, i, j;
while (scanf("%d", &n) != EOF)
{
if (!n)
break;
if (n == 1)
{
puts("1");
continue;
}
length = 1;
a[1] = 1;
// (n + 2) * (n + 3)...(2 * n)
for (i = n + 2; i <= 2 * n; i++)
{
carry = 0;
for (j = 1; j <= length; j++)
{
carry += a[j] * i;
a[j] = carry % 10;
carry /= 10;
}
while (carry)
{
a[++length] = carry % 10;
carry /= 10;
}
}
//formatting printing
for (i = length; i >= 1; i--)
{
printf("%d", a[i]);
}
puts("");
}
return 0;
}


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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: