zoukankan      html  css  js  c++  java
  • 征战蓝桥 —— 2015年第六届 —— C/C++A组第6题——牌型种数

    牌型种数

    小明被劫持到X赌城,被迫与其他3人玩牌。
    一副扑克牌(去掉大小王牌,共52张),均匀发给4个人,每个人13张。
    这时,小明脑子里突然冒出一个问题:
    如果不考虑花色,只考虑点数,也不考虑自己得到的牌的先后顺序,自己手里能拿到的初始牌型组合一共有多少种呢?

    请填写该整数,不要填写任何多余的内容或说明文字。

    代码

    #include <iostream>
    #include <sstream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    string pai[13];
    
    int countOf(vector<string> path, string p) {
        int ans = 0;
        for (int i = 0; i < path.size(); ++i) {
            if (path[i] == p)
                ans++;
        }
        return ans;
    }
    
    int ans;
    
    void f(int k, vector<string> path) {
    
        if (k == 0)ans++;
        for (int i = 0; i < 13; ++i) {
            if (countOf(path, pai[i]) == 4)continue;
            path.push_back(pai[i]);//拼接,代表采纳这张牌
            f(k - 1, path);
            path.erase(path.end() - 1);
        }
    }
    
    void i2s(int num, string &str) {
        stringstream ss;
        ss << num;
        ss >> str;
    }
    
    int main(int argc, const char *argv[]) {
        for (int i = 1; i <= 13; ++i) {
            i2s(i, pai[i - 1]);
        }
        vector<string> v;
        f(13, v);
        cout << ans << endl;
        return 0;
    }
    

    递归加回溯会超时。

    #include <iostream>
    #include <sstream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    
    int ans;
    
    void f(int k,int cnt) {
        if(cnt>13||k>13)return;
        if(k==13&&cnt==13){
            ans++;
            return;
        }
        for (int i = 0; i < 5; ++i) {
            f(k+1,cnt+i);
        }
    }
    
    
    int main(int argc, const char *argv[]) {
        f(0, 0);
        cout << ans << endl;
        return 0;
    }
    
    
  • 相关阅读:
    Qt之QFileSystemWatcher
    office2007-安装程序找不到office.zh-cn*.文件
    Maven父子项目配置-多模块(multi-modules)结构
    Maven项目打包,Jar包不更新的问题
    开发Spring Shell应用程序
    Spring Shell参考文档
    Spring Shell介绍
    maven项目打包时生成dependency-reduced-pom.xml
    使用VBA批量转换Excel格式,由.xls转换成.xlsx
    修改MyEclipse取消默认工作空间
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12338960.html
Copyright © 2011-2022 走看看