这道题刚开始尝试用字符串来做,后来发现答案中直接转为字典顺序,并通过模除移位获取对战结果,过程如下
#include <iostream>
using namespace std;
// 手势转为数字
int change(char c){
if(c == 'B'){
return 0;
}else if(c == 'C'){
return 1;
}else if(c == 'J'){
return 2;
}
}
int main(){
int n, k1, k2;
char c1, c2;
char mp[3] = {'B', 'C', 'J'};
int times_A[3] = {0, 0, 0}, times_B[3] = {0, 0, 0}, hand_A[3] = {0, 0, 0}, hand_B[3] = {0, 0, 0};
cin >> n;
while(n-- > 0){
cin >> c1;
cin >> c2;
k1 = change(c1);
k2 = change(c2);
// 通过模除移位,判断甲乙胜负
if((k1 + 1) % 3 == k2){
// 甲胜
times_A[0]++;
times_B[2]++;
// 甲的该手势获胜次数加1
hand_A[k1]++;
}else if(k1 == k2){
// 二者平
times_A[1]++;
times_B[1]++;
}else{
// 乙胜
times_A[2]++;
times_B[0]++;
// 乙的该手势获胜次数加1
hand_B[k2]++;
}
}
int i = 1;
int idx_a = 0, idx_b = 0;
// 找出甲乙获胜次数最多的手势
while(i < 3){
if(hand_A[i] > hand_A[idx_a]){
idx_a = i;
}
if(hand_B[i] > hand_B[idx_b]){
idx_b = i;
}
++i;
}
cout << times_A[0] << ' ' << times_A[1] << ' ' << times_A[2] << endl;
cout << times_B[0] << ' ' << times_B[1] << ' ' << times_B[2] << endl;
cout << mp[idx_a] << ' ' << mp[idx_b] << endl;
return 0;
}