题解 | #扑克牌大小#
扑克牌大小
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; } }