http://acm.nyist.net/JudgeOnline/problem.php?pid=1103
区域赛系列一多边形划分
时间限制:1000 ms | 内存限制:65535 KB
难度:2
- 描述
Give you a convex(凸边形), diagonal n-3 disjoint divided into n-2 triangles(直线), for different number of methods, such as n=5, there are 5 kinds of partition method, as shown in Figure
- 输入
- The first line of the input is a n (1<=n<=1000), expressed n data set.
The next n lines each behavior an integer m (3<=m<=18), namely the convex edges. - 输出
- For each give m,, output how many classification methods.
example output: Case #a : b - 样例输入
3 3 4 5
- 样例输出
Case #1 : 1 Case #2 : 2 Case #3 : 5
- 提示
- Catalan number
解题思路: 卡特兰数
1 #include <stdio.h>
2 #include <string.h>
3
4 int Catalan(int n)
5 {
6 if(n <= 1)
7 return 1;
8
9 int *h = new int [n+1]; //保存临时结果
10 h[0] = h[1] = 1; //h(0)和h(1)
11 for(int i = 2; i <= n; i++) //依次计算h(2),h(3)...h(n)
12 {
13 h[i] = 0;
14 for(int j = 0; j < i; j++) //根据递归式计算 h(i)= h(0)*h(i-1)+h(1)*h(i-2) + ... + h(i-1)h(0)
15 h[i] += (h[j] * h[i-1-j]);
16 }
17 int result = h[n]; //保存结果
18 delete [] h; //注意释放空间
19 return result;
20 }
21
22 int main(){
23 int c, x;
24 int Case = 1;
25 scanf("%d", &c);
26 while(c--){
27 scanf("%d", &x);
28 printf("Case #%d : %d ", Case++, Catalan(x - 2));
29 }
30 return 0;
31 }
3
4 int Catalan(int n)
5 {
6 if(n <= 1)
7 return 1;
8
9 int *h = new int [n+1]; //保存临时结果
10 h[0] = h[1] = 1; //h(0)和h(1)
11 for(int i = 2; i <= n; i++) //依次计算h(2),h(3)...h(n)
12 {
13 h[i] = 0;
14 for(int j = 0; j < i; j++) //根据递归式计算 h(i)= h(0)*h(i-1)+h(1)*h(i-2) + ... + h(i-1)h(0)
15 h[i] += (h[j] * h[i-1-j]);
16 }
17 int result = h[n]; //保存结果
18 delete [] h; //注意释放空间
19 return result;
20 }
21
22 int main(){
23 int c, x;
24 int Case = 1;
25 scanf("%d", &c);
26 while(c--){
27 scanf("%d", &x);
28 printf("Case #%d : %d ", Case++, Catalan(x - 2));
29 }
30 return 0;
31 }