【最长的顺子】斗地主起源于湖北十堰房县,据说是一位叫吴修全的年轻人根据当地流行的扑克玩法“跑得快”改编的,如今已风靡整个中国,并流行于互联网上。

该博客介绍了一个用于检测扑克牌游戏中玩家可能构成的最长顺子的算法。算法首先将牌面转换为整数,然后通过双指针法找到连续的牌序列,并检查是否能构成顺子。最后,输出满足条件的最长顺子。示例中展示了输入和输出的处理过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

牌型 : 单顺,又称顺子,最少5张牌,最多12张牌(3…A)不能有2,也不能有大小王,不计花色。
例如 3 - 4 - 5 - 6 - 7 - 8,7 - 8 - 9 - 10 - J - Q,3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - J - Q - K - A
可用的牌 3 < 4 < 5 < 6 < 7 < 8 < 9 < 10 < J < Q < K < A < 2 < B(小王) < C(大王),每种牌除大小王外有四种花色(共有13 * 4 + 2张牌)
输入:1、手上有的牌 2、已经出过的牌(包括对手出的和自己出的牌)
输出:对手可能构成的最长的顺子(如果有相同长度的顺子,输出牌面最大的那个那一个),如果无法构成顺子,则输出 NO - CHAIN
输入描述 :
输入的第一行为当前手中的牌
输入的第二行为已经出过的牌
输出描述 :
最长的顺子
示例 :
输入
3-3-3-3-4-4-5-5-6-7-8-9-10-J-Q-K-A
4-5-6-7-8-8-8
输出
9 - 10 - J - Q - K - A

string long_shunzi()
{
    map<string, int> strtoint;
    strtoint[string("3")] = 0;
    strtoint[string("4")] = 1;
    strtoint[string("5")] = 2;
    strtoint[string("6")] = 3;
    strtoint[string("7")] = 4;
    strtoint[string("8")] = 5;
    strtoint[string("9")] = 6;
    strtoint[string("10")] = 7;
    strtoint[string("J")] = 8;
    strtoint[string("Q")] = 9;
    strtoint[string("K")] = 10;
    strtoint[string("A")] = 11;
    map<int, string> inttostr;
    inttostr[0] = "3";
    inttostr[1] = "4";
    inttostr[2] = "5";
    inttostr[3] = "6";
    inttostr[4] = "7";
    inttostr[5] = "8";
    inttostr[6] = "9";
    inttostr[7] = "10";
    inttostr[8] = "J";
    inttostr[9] = "Q";
    inttostr[10] = "K";
    inttostr[11] = "A";
    vector<int> vec;
    vector<pair<int, int>> vecres;
    for (int i = 0; i < 12; ++i) {
        vec.push_back(int(4));
    }
    string str1;
    string str2;
    cin >> str1;
    cin >> str2;
    // 除去已知的牌
    string cs = "";
    for (auto c : str1) {
        if (c != '-') {
            cs += c;
        } else {
            vec[strtoint[cs]]--;
            cs = "";
        }
    }
    vec[strtoint[cs]]--;
    cs = "";
    for (auto c : str2) {
        if (c != '-') {
            cs += c;
        } else {
            vec[strtoint[cs]]--;
            cs = "";
        }
    }
    vec[strtoint[cs]]--;
    cs = "";

    // 双指针找连续子序列
    for (int i = 0; i < 12; ++i) {
        int right = i;
        while (right < 12) {
            if (vec[right] > 0) {
                right++;
            } else {
                break;
            }                
        }
        if (right - i >= 5) {
            vecres.push_back(make_pair(i, right));
        }
        i = right;
    }
    // 连续子序列找满足条件的 1、最长的2、 同长牌面大的
    int flag = 0;
    int max = 0;
    int j = 0;
    for (auto ele : vecres) {
        int count = ele.second - ele.first;
        if (max <= count) {
            max = count;
            flag = j;
        }
        j++;
    }
    int fir = vecres[flag].first;
    int sec = vecres[flag].second;
    string outsr;
    for (int i = fir; i < sec; ++i) {
        outsr += inttostr[i];
        if (i != sec - 1) {
            outsr += "-";
        }
    }
    cout << outsr << endl;
    return outsr;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值