卡特兰数:2N个人排队买电影票,N个人持5元买票,N个人持10元买票.售票处在售票前只有票没有钱,票价5元 ,问有多少种排队方式 能让2N个人顺利买票,并且输出所有排队队列(不会因为找钱问题)

分析

n = 0 时,队列为空,可以认为只有一种;

n = 1 时,队列:5 ,10  共有1种;

n = 2 时 ,队列: 5, 10,5,10

                         5,5,10,10   共有两种

n = 3时, 队列:5,5,5,10,10,10

                        5,5,10,5,10,10

                        5,5,10,10,5,10

                        5,10,5,5,10,10

                        5,10,5,10,5,10    共有5种;

我门可以认为符合卡特兰数,1,1,2,5,14,42.。。。

一、输出序列

    接下来时验证,小编在做n=3的情况是,发现可以认为有两个容器,一个全部都放5元的票,另一个全部放10元的票,这里遵循三个规则:

    1、当5元容器和10元容器全部为空时,输出序列

    2、5元容器不为空时,5元出容器

    3、10元容器不为空并且10元容器的个数必须大于5元容器的个数(也就是当10元容器的个数和5元容器的个数相等时,10不能出容器)

//

//  main.cpp

//  CSDN.卡特兰数:售票序列问题

//

//  Created by Mr Gao on 2018/4/12.

//  Copyright © 2018年 Mr Gao. All rights reserved.

//


#include <iostream>

#include <vector>

#include <string>

using namespace std;


void genAns(int left, int right, string path, vector<string> &ans){

    if(left == 0 && right == 0){  //规则一

        path.pop_back(); 

        ans.push_back(path);

    }

    if(left != 0)                 //规则二

        genAns(left - 1, right, path + "5-", ans);

    if(right != 0 && left < right)      //规则三

        genAns(left, right - 1, path + "10-", ans);

}


int main(int argc, const char * argv[]) {

    // insert code here...

    int n;

    while(cin>>n){

        string path;

        vector<string> ans;

        genAns(n, n, path, ans);

        for(int i = 0; i < ans.size(); i++)

            std::cout << ans[i] <<endl;

        cout<<"count: "<<ans.size()<<endl;

    }

    return 0;

}


代码输出:

1

5-10

count: 1

2

5-5-10-10

5-10-5-10

count: 2

3

5-5-5-10-10-10

5-5-10-5-10-10

5-5-10-10-5-10

5-10-5-5-10-10

5-10-5-10-5-10

count: 5

4

5-5-5-5-10-10-10-10

5-5-5-10-5-10-10-10

5-5-5-10-10-5-10-10

5-5-5-10-10-10-5-10

5-5-10-5-5-10-10-10

5-5-10-5-10-5-10-10

5-5-10-5-10-10-5-10

5-5-10-10-5-5-10-10

5-5-10-10-5-10-5-10

5-10-5-5-5-10-10-10

5-10-5-5-10-5-10-10

5-10-5-5-10-10-5-10

5-10-5-10-5-5-10-10

5-10-5-10-5-10-5-10

count: 14

    根据程序输出结果可以验证是符合卡特兰数:

二、下面给大家写一个卡特兰数的输出程序:

    根据卡特兰数的公式:C(2n, n) / (n+1)  =  (2n)! / ( n! * (n+1)! )

//

//  main.cpp

//  CSDN. 卡特兰数

//

//  Created by Mr Gao on 2018/4/12.

//  Copyright © 2018年 Mr Gao. All rights reserved.

//


#include <iostream>

using namespace std;

int main(int argc, const char * argv[]) {

    // insert code here...

    int n;

    while(cin >> n){

        if(n == 1 || n == 0)

            cout<<1<<endl;

        double ans = 1;

        for(int i = 1; i <= n; i++){

            ans = ans * (n + i) / i;

        }

        cout<< ans / (n+1) << endl;

    }

    return 0;

}


结果展示:

0

result:1

1

result:1

2

result:2

3

result:5

4

result:14

5

result:42

6

result:132

7

result:429

8

result:1430


小编的第一篇博客,写了好久,希望对大家有帮助。

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值