比较考验各种情况的讨论 是否有遗漏和重复 很好的一道练习题
#include <iostream> #include <sstream> #include <vector> #include <unordered_map> using namespace std; int main() { vector<string> cards; string str, token; getline(cin, str); stringstream ss(str); while(getline(ss, token, '-')) { cards.push_back(token); } //把两副牌的每张牌再拆开 vector<string> card1; vector<string> card2; stringstream ss1(cards[0]); while(getline(ss1, token, ' ')) { card1.push_back(token); } stringstream ss2(cards[1]); while(getline(ss2, token, ' ')) { card2.push_back(token); } //两幅手牌里面存在对王直接输出对王 if(cards[0] == "joker JOKER" || cards[1] == "joker JOKER") { cout << "joker JOKER" << endl; return 0; } //若没有对王 讨论是否有炸弹 //如果两个都是炸弹 则可以和对子 单张等放在一起处理 因为只需要比较第一张牌 //所以讨论只有一个炸弹的情况 if(card1.size() == 4 && card2.size() != 4) { cout << cards[0] << endl; return 0; } else if(card1.size() != 4 && card2.size() == 4) { cout << cards[1] << endl; return 0; } //如果没有王炸和炸弹 则首先需要要求牌数一样 if(card1.size() != card2.size()) { cout << "ERROR" << endl; return 0; } //剩下的牌数都一样了 //定义一个umap用来记录大小 unordered_map<string, int> umap = { {"3", 1}, {"4", 2}, {"5", 3}, {"6", 4}, {"7", 5}, {"8", 6}, {"9", 7}, {"10", 8}, {"J", 9}, {"Q", 10}, {"K", 11}, {"A", 12}, {"2", 13}, {"joker", 14}, {"JOKER", 15} }; //个子 对子 三个 炸弹 顺子都只需要比较第一张牌就行 可以好好想想 if(umap[card1[0]] > umap[card2[0]]) { cout << cards[0] << endl; } else { cout << cards[1] << endl; } return 0; }