题解 | #扑克牌大小#
扑克牌大小
http://www.nowcoder.com/practice/d290db02bacc4c40965ac31d16b1c3eb
#include <iostream> #include <algorithm> using namespace std; int main() { string cards = "345678910JQKA2jokerJOKER"; string input_cards; while(getline(cin, input_cards)) { string left; string right; // 双王必定是最大的 if (input_cards.find("joker JOKER") != string::npos) { cout << "joker JOKER" << endl; } else if(input_cards.find("JOKER joker") != string::npos) { cout << "JOKER joker" << endl; } else { // 分割左右手牌,计算空格数用于判断手牌类型 left = input_cards.substr(0, input_cards.find('-')); right = input_cards.substr(input_cards.find('-') + 1); int blank_left = count(left.begin(), left.end(), ' '); int blank_right = count(right.begin(), right.end(), ' '); // 空格数相同意味着手牌类型相同,根据第一个拍的大小进行比较 if (blank_left == blank_right) { if (cards.find(left.substr(0, left.find(' '))) > cards.find(right.substr(0, right.find(' ')))) { cout << left << endl; } else { cout << right << endl; } // 手牌类型不同则检查是否有炸弹,有则输出,没有则无法进行比较输出错误 } else { if (blank_left == 3) { cout << left << endl; } else if(blank_right == 3) { cout << right << endl; } else { cout << "ERROR" << endl; } } } } return 0; }