标签:
输入两手牌,两手牌之间用“-”连接,每手牌的每张牌以空格分隔,“-”两边没有空格,如4 4 4 4-joker JOKER。
输出两手牌中较大的那手,不含连接符,扑克牌顺序不变,仍以空格隔开;如果不存在比较关系则输出ERROR。
4 4 4 4-joker JOKER
joker JOKER
#include <iostream> #include <string> #include <sstream> #include <vector> using namespace std; string poker[]= {"3","4","5","6","7","8","9","10","J","Q","K","A","2","joker","JOKER"}; void stringTovector(const string s, vector<string>& v) { stringstream ss(s); string tmp; while(ss >> tmp) { v.push_back(tmp); //cout << tmp <<endl; //cout << v.size() <<endl; } } bool isJoker(vector<string> v) { if(v.size() == 2 && v[0] == "joker" && v[1] == "JOKER") return true; else return false; } bool isBomb(vector<string> v) { if(v.size()==4 && v[0] == v[1] && v[1] == v[2] && v[2] == v[3]) return true; else return false; } int getLeastNumPosition(string s)//找出最小牌在数组poker中的位置 { for(int n=0; n<15; n++) if(poker[n] == s) return n; return -1; } int main() { //string poker[]= {"3","4","5","6","7","8","9","10","J","Q","K","A","2","joker","JOKER"}; string s1, s2,s; while(getline(cin, s)) { int n = s.find("-"); s1 = s.substr(0,n); s2 = s.substr(n+1,s.size()); //cout << s1 << endl << s2 << endl; vector<string> v1,v2; stringTovector(s1,v1); //cout << v1.size() << endl; stringTovector(s2, v2); int len1 = v1.size(); int len2 = v2.size(); //cout << len1 << endl; if(len1 != len2)//类型不同,必须有一个为对王或者有炸弹 { if(isJoker(v1)) { cout << s1 << endl; //continue; } else if(isJoker(v2)) { cout << s2 << endl; //continue; } else if(isBomb(v1)) { cout << s1 << endl; //continue; } else if(isBomb(v2)) { cout << s2 << endl; //continue; } else { cout << "ERROR" << endl; } } else //牌数相等,类型相同,题目保证 { int n1,n2; n1 = getLeastNumPosition(v1[0]); n2 = getLeastNumPosition(v2[0]); if(n1 < n2) cout << s2 << endl; else cout << s1 << endl; } } return 0; }
标签:
原文地址:http://www.cnblogs.com/luolizhi/p/5780594.html