题解:
#include<cstdio> #include<iostream> #include<map> using namespace std; int main() { //分别保存两人布锤剪的胜平负次数 int countA[3][3] = { 0 }; int countB[3][3] = { 0 }; int n; char a, b; map<char, int> fun; fun['B'] = 0; fun['C'] = 1; fun['J'] = 2; cin >> n; for (int i = 0; i < n; i++) { cin >> a >> b; if (a == b) { countA[fun[a]][1]++; countB[fun[b]][1]++; } else { if (a == 'B'&&b == 'C') { countA[0][0]++; countB[1][2]++; } else if (a == 'C'&&b == 'B') { countA[1][2]++; countB[0][0]++; } else if (a == 'C'&&b == 'J') { countA[1][0]++; countB[2][2]++; } else if (a == 'J'&&b == 'C') { countA[2][2]++; countB[1][0]++; } else if (a == 'B'&&b == 'J') { countA[0][2]++; countB[2][0]++; } else { countA[2][0]++; countB[0][2]++; } } } //统计胜平负次数 int temp = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) temp += countA[j][i]; if (i != 2) cout << temp << " "; else cout << temp; temp = 0; } cout << endl; temp = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) temp += countB[j][i]; if (i != 2) cout << temp << " "; else cout << temp; temp = 0; } cout << endl; //统计手势赢得次数 if (countA[0][0] >= countA[1][0] && countA[0][0] >= countA[2][0]) cout << "B "; else if (countA[1][0] > countA[0][0] && countA[1][0] >= countA[2][0]) cout << "C "; else cout << "J "; if (countB[0][0] >= countB[1][0] && countB[0][0] >= countB[2][0]) cout << "B"; else if (countB[1][0] > countB[0][0] && countB[1][0] >= countB[2][0]) cout << "C"; else cout << "J"; return 0; } //此题没有什么卡住的点,重要的是细心 //另外题目要求解不唯一时按字典序输出,可以采取像我这样按布锤剪排 //判断胜负时用了很多if else,也可以考虑用map把字母转成数字,会简洁一些