题解 | #扑克牌大小#
扑克牌大小
https://www.nowcoder.com/practice/d290db02bacc4c40965ac31d16b1c3eb
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
//给手牌分类。通过系统调用getline函数的次数判断牌的数量,根据数量划分类别(其中将双王定位0类)。
int judge_class(string str) {
int count = 0;
istringstream s(str);
string tmp;
while (getline(s, tmp, ' ')) {
count++;
}
if (count == 2 && (tmp == "joker" || tmp == "JOKER")) {
return 0;
} else {
return count;
}
}
//将第一张牌传递给str,根据规则赋予不同权重。特别注意A,2,大小joker,10
int get_value(string str) {
int value;
if (str == "A") {
value = (int)'A' + 26;
} else if (str == "2") {
value = (int)'2' + (int)'A';
} else if (str == "K") {
value = (int)'K' + 8;
} else if (str == "10") {
value = (int)'9' + 1;
} else if (str == "joker") {
value = (int)'2' + (int)'A' + 1;
} else if (str == "JOKER") {
value = (int)'2' + (int)'A' + 2;
} else {
value = (int)str[0];
}
return value;
}
int main() {
string input;
while (getline(cin, input)) {
vector<pair<string, int>> v;
istringstream ss(input);
string temp;
//将(手牌,字符串)作为pair记录进vector中
while (getline(ss, temp, '-')) {
v.push_back(make_pair(temp, judge_class(temp)));
}
//将两幅手牌各自的第一张牌赋给left,right
string left, right;
istringstream s0(v[0].first);
getline(s0, left, ' ');
istringstream s1(v[1].first);
getline(s1, right, ' ');
//计算牌的权重
int left_value = get_value(left);
int right_value = get_value(right);
//分类讨论,输出结果
if (v[0].second == 0 && v[1].second == 0) { //两副手牌均为双大小王
cout << "ERROR" << endl;
} else if (v[0].second == 0) { //一方为双大小王
cout << v[0].first << endl;
} else if (v[1].second == 0) {
cout << v[1].first << endl;
} else if (v[0].second == 4 && v[1].second == 4) { //双方为炸弹
if (left_value > right_value) {
cout << v[0].first << endl;
} else if (left_value < right_value) {
cout << v[1].first << endl;
} else {
cout << "ERROR" << endl;
}
} else if (v[0].second == 4) { //一方为炸弹
cout << v[0].first << endl;
} else if (v[1].second == 4) {
cout << v[1].first << endl;
} else if (v[0].second == v[1].second) { //两副牌为同类型
if (left_value > right_value) {
cout << v[0].first << endl;
} else if (left_value < right_value) {
cout << v[1].first << endl;
} else {
cout << "ERROR" << endl;
}
} else { //其他情况(非同类型,无炸弹,无双王)
cout << "ERROR" << endl;
}
}
}
// 64 位输出请用 printf("%lld")
