扑克牌大小问题
解题思路: 先判断是否有王炸,有王炸直接输出王炸,然后依序遍历是否有4张相同的炸弹,一边有一边没有则有炸弹的一方牌大,最后判断同种类型的牌面大小,个牌,对牌和三个
代码实现:
#include<iostream>
#include<string>
using namespace std;
int num(string s){
int res = 1;
int n = s.find(' ');
while (n != -1){
s = s.substr(n + 1);
n = s.find(' ');
res++;
}
return res;
}
int main(){
string s;
string p = "345678910JQKA2";
while (getline(cin, s)){
int n = s.find('-');
string s1 = s.substr(0, n);
string s2 = s.substr(n + 1);
string m = "joker JOKER";
if (s1.find(m) != -1 || s2.find(m) != -1){
cout << m << endl;
continue;
}
int n1 = num(s1), n2 = num(s2);
if (n1 == 4 && n2 != 4){
cout << s1 << endl;
}
else if (n1 != 4 && n2 == 4) {
cout << s2 << endl;
}
else if (n1 != n2) {
cout << "ERROR" << endl;
}
else if (n1 == 1){
if (p.find(s1) < p.find(s2)){
cout << s2 << endl;
}
else {
cout << s1 << endl;
}
}
else {
int p1 = p.find(s1.substr(0, s1.find(' ')));
int p2 = p.find(s2.substr(0, s2.find(' ')));
if (p1 < p2){
cout << s2 << endl;
}
else {
cout << s1 << endl;
}
}
}
return 0;
}
参考答案: