JOJ2026:Divide the Birthday Cake

On L.X.'s birthday, I bought her a big Birthday Cake, and she felt very happy.
"So how do you divide the cake?" I asked.
"I do not know. I do not want to simply divide it into several sectors. Hmn, Let me see. The cake is a little strange. Its shape looks like a polygon. En, I want to divide it into small pieces of triangles. I mean, if there are N sides, there should be N-2 small triangles. We can use N-3 diagonal(s) which do not intersect inside of the polygon to divide the cake into N-2 triangles." She said.
"Yeah, of course we can. It is an easy job." I took out a pencil and drew a dividing method on the paper. 
"Yes. But I want to know how many different methods to divide the cake?" She smiled.
"Ah, it is not difficult. I will tell you the answer later!" said.
"Ok, but if you do not get the answer at last, you cannot eat the cake!"
"Ok…"

Then I began to calculate the number and she began to cut the cake. But I didn't get the answer, for there were 16 vertexes. Oh, I was hungry! You must help me!!!

Here are the examples of "VALID" and "INVALID" dividing method.

1003

Input

There are several lines in the input file, each line contains a number N stands for the number the vertex of the polygon. 5<=N<=31. Your program should proceed to the end of the file.

Output

Print the number of dividing method one per line.

Sample Input

5
6

Sample Output

5
14


题目大意:多边形分割成三角形的方案数。

思路:典型的卡特兰数,按照卡特兰数公式求解一下就行了。卡特兰数详情见这里

代码:

#include <cstdio>
using namespace std;

int main()
{
    int num;
    while (scanf("%d", &num) != EOF)
    {
        int n, k;
        double res = 1.0;
        num--;
        n = 2 * num - 2;
        k = num - 1;
        for (; k >= 1; --k)
        {
            res *= 1.0 * n / k;
            n--;
        }
        res /= 1.0 * num;
        printf("%.lf\n", res);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值