题解 | #扑克牌大小#

扑克牌大小

https://www.nowcoder.com/practice/d290db02bacc4c40965ac31d16b1c3eb

#include<iostream>
#include<string>
using namespace std;

int getvalue(string s) { //根据输入的字符首字母输出大小等级
    switch (s[0]) {
        case '3':
            return 1;
        case '4':
            return 2;
        case '5':
            return 3;
        case '6':
            return 4;
        case '7':
            return 5;
        case '8':
            return 6;
        case '9':
            return 7;
        case '1':
            return 8; //用1代替10
        case 'J':
            return 9;
        case 'Q':
            return 10;
        case 'K':
            return 11;
        case 'A':
            return 12;
        case '2':
            return 13;
    }
    return 0;
}

int main() {
    string s;
    while (getline(cin, s)) {
        string s1 = s.substr(0, s.find('-')); //从-处截取成两段
        string s2 = s.substr(s.find('-') + 1);
        int space1 = 0, space2 = 0;
        for (int i = 0; i < s1.length(); i++) //统计字符串中空格的数量
            if (s1[i] == ' ')
                space1++;
        for (int i = 0; i < s2.length(); i++)
            if (s2[i] == ' ')
                space2++;
        if (s1 == "joker JOKER" ||
                s2 == "joker JOKER") //如果有王炸直接输出王炸
            cout << "joker JOKER";
        else if (space1 ==  3 &&
                 space2 == 3) { //都有3个空格,说明4张牌,说明两个都是炸弹
            if (getvalue(s1) > getvalue(s2)) //比较炸弹大小
                cout << s1 << endl;
            else
                cout << s2 << endl;
        } else if (space1 ==
                   3) //字符串其中一个空格为3,说明一个是炸弹,输出炸弹
            cout << s1 << endl;
        else if (space2 == 3)
            cout << s2 << endl;
        else if (space1 == space2) { //没有炸弹的情况下相同类型才能比较

            if (s1.length() == 1 && (s2 == "joker" || s2 == "JOKER"))
                cout << s2 << endl;
            else if (s2.length() == 1 && (s1 == "joker" || s1 == "JOKER"))
                cout << s1 << endl;
            else if (getvalue(s1) > getvalue(
                         s2)) //个子、对子、三个、顺子都是比较第一个大小
                cout << s1 << endl;
            else
                cout << s2 << endl;
        } else //无法比较
            cout << "ERROR" << endl;
    }
}

全部评论

相关推荐

码砖:求职岗位要突出,一眼就能看到,教育背景放到最后,学校经历没那么重要,项目要重点突出
点赞 评论 收藏
分享
05-29 20:34
门头沟学院 C++
KarlAllen:得做好直接春招的准备。学历差的话,一是面试要求会比学历好的严格不少,二是就算面试通过了也会被排序。总之暑期和秋招对于学历差的就是及其不友好
无实习如何秋招上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务