题解 | #扑克牌大小#

扑克牌大小

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

#include <iostream>
#include <string>
#include <vector>

std::vector<std::string> split(std::string str, char c) {
    int pos;
    std::string s(str);
    std::vector<std::string> tmp;
    while (!s.empty()) {
        if ((pos = s.find(c)) != s.npos) {
            tmp.push_back(s.substr(0, pos));
            s = s.substr(pos + 1);
        } else {
            tmp.push_back(s);
            break;
        }
    } 
    return tmp;
}

int judge(std::vector<std::string> vec) {
    int size = vec.size();
    if (size == 2) {
        if (vec[0] == vec[1]) {
            return 2;
        } else if ((vec[0] == "joker" && vec[1] == "JOKER")
                   || (vec[1] == "joker" && vec[2] == "JOKER")) {
            return 6;
        }
    } else {
        return size;
    }
}

void print(std::vector<std::string> vec) {
    for (auto it = vec.begin(); it < vec.end(); it++) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;
}

int main() {
    std::string str;
    std::string poker = "345678910JQKA2";
    while (getline(std::cin, str)) {
        std::vector<std::string> vec0(split(str, '-'));
        std::vector<std::string> vec1(split(vec0[0], ' '));
        std::vector<std::string> vec2(split(vec0[1], ' '));
        if (judge(vec1) == 6) {
            print(vec1);
            continue;
        }
        if (judge(vec2) == 6) {
            print(vec2);
            continue;
        }
        if (judge(vec1) == 4) {
            if (judge(vec2) == 4 && (poker.find(vec2[0]) >  poker.find(vec2[0]))) {
                print(vec2);
            } else {
                print(vec1);
            }
            continue;
        }
        if (judge(vec2) == 4) {
            print(vec2);
            continue;
        }
        if (judge(vec1) != judge(vec2)) {
            std::cout << "ERROR" << std::endl;
            continue;
        }

        if (poker.find(vec1[0]) > poker.find(vec2[0])) {
            print(vec1);
        } else {
            print(vec2);
        }
    }
}
全部评论

相关推荐

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