题解 | #扑克牌大小#

扑克牌大小

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

#include <cstdio>
#include <iostream>
#include <sstream>
using namespace std;
#include <string>
#include <istream>
bool isboom(string str) {
    istringstream is(str);
    string a;
    string b;
    string c;
    string d;
    getline(is, a, ' ');
    getline(is, b, ' ');
    getline(is, c, ' ');
    getline(is, d);
    if (a == b && a == b && a == c && a == d) {
        return true;
    } else {
        return false;
    }
}

bool isVaild(string str1, string str2) {
    int cnt1 = 0;
    int cnt2 = 0;
    string str;
    istringstream is1(str1);
    istringstream is2(str2);
    while (is1 >> str) {
        cnt1++;
    }
    while (is2 >> str) {
        cnt2++;
    }
    if (cnt1 == cnt2) {
        return true;
    } else {
        return false;
    }
}

bool compAbigB(char a, char b) {
    if (a == '2') {
        return true;
    }
    if (b == '2') {
        return false;
    }
    if (a == 'A') {
        return true;
    }
    if (b == 'A') {
        return false;
    }
    if (a == 'K') {
        return true;
    }
    if (b == 'K') {
        return false;
    }
    if (a == 'Q') {
        return true;
    }
    if (b == 'Q') {
        return false;
    }
    if (a == 'J') {
        return true;
    }
    if (b == 'J') {
        return false;
    }
    if (a == 't') {
        return true;
    }
    if (b == 't') {
        return false;
    }

    if (a > b) {
        return true;
    } else {
        return false;
    }

}

int main() {
    string str1;
    string str2;
    getline(cin, str1, '-');
    getline(cin, str2);
    if (str1 == "joker JOKER") {
        cout << str1 << endl;
    } else if (str2 == "joker JOKER") {
        cout << str2 << endl;
    } else {
        if (isboom(str1) && !isboom(str2)) {
            cout << str1 << endl;
        } else if (!isboom(str1) && isboom(str2)) {
            cout << str2 << endl;
        } else if (isVaild(str1, str2)) {
            string w1, w2;
            char c1, c2;
            istringstream is1(str1);
            istringstream is2(str2);
            is1 >> w1;
            is2 >> w2;
            if (w1 == "10") {
                c1 = 't';
            } else {
                c1 = str1[0];
            }
            if (w2 == "10") {
                c2 = 't';
            } else {
                c2 = str2[0];
            }
            if (compAbigB(c1, c2)) {
                cout << str1 << endl;
            } else {
                cout << str2 << endl;
            }
        } else {
            cout << "ERROR" << endl;
        }
    }


}

感觉挺没意义的一道题

几个需要注意的点:数字里有一个10,不能简单用str[0]进行比较

需要比较牌形是否合法,也因为10的存在不能靠比较字符串长度,就很烦

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务