博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
杭电_ACM_Count the Trees
阅读量:6095 次
发布时间:2019-06-20

本文共 2926 字,大约阅读时间需要 9 分钟。

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
1210250
 
Sample Output
146094932480075414671852339208296275849248768000000
View Code
1 #include 
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 printing34 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.

转载于:https://www.cnblogs.com/chuanlong/archive/2012/11/07/2758381.html

你可能感兴趣的文章
Codeforces 520B:Two Buttons(思维,好题)
查看>>
web框架-(二)Django基础
查看>>
Jenkins持续集成环境部署
查看>>
emoji等表情符号存mysql的方法
查看>>
ubuntu14.04中国源
查看>>
Excel到R中的日期转换
查看>>
网络层
查看>>
centos7没有ifconfig命令
查看>>
10-SAP PI开发手册-ERP发布服务供外围系统调用(RFC类型)
查看>>
cmd命令行查看windows版本
查看>>
城市三联动简单实例
查看>>
opencv边缘检测的入门剖析(第七天)
查看>>
Spring Boot☞ 使用Thymeleaf模板引擎渲染web视图
查看>>
mac本地搭建wordpress
查看>>
CSS3学习手记(2) 伪类选择器
查看>>
DPS首战鞍山
查看>>
Microsoft Sync Framework基础篇 2:Microsoft Sync Framework架构与运行时
查看>>
Git 常用命令大全
查看>>
Xilinx ISE 12.4的简单应用
查看>>
eclipse中访问不了tomcat首页server Locations变灰无法编辑
查看>>