题目链接:
https://pintia.cn/problem-sets/994805260223102976/problems/994805304020025344
我的代码:
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
using namespace std;
typedef pair<char, int> PAIR;
ostream& operator<<(ostream& out, const PAIR& p) {
return out << p.second << "\t" << p.second;
}
int main() {
int n;
cin >> n;
int result[3] = { 0 };
int C_A = 0, B_A = 0, J_A = 0;
int C_B = 0, B_B = 0, J_B = 0;
//result[0],甲赢,
//result[1],平局
//result[2],乙赢
while (n--) {
char a, b;
cin >> a >> b;//a--甲方,b--乙方
if (a == 'C'&&b == 'J') {
result[0]++;
C_A++;
}
else if (a == 'C'&&b == 'B') {
result[2]++;
B_B++;
}
else if (a == 'B'&&b == 'C') {
result[0]++;
B_A++;
}
else if (a == 'B'&&b == 'J') {
result[2]++;
J_B++;
}
else if (a == 'J'&&b == 'C') {
result[2]++;
C_B++;
}
else if (a == 'J'&&b == 'B') {
result[0]++;
J_A++;
}
else {
result[1]++;
}
////输出次数
//char max_a;
//int max_A = max(max(B_A,C_A), J_A);
}
cout << result[0] << " " << result[1] << " " << result[2] << endl;
cout << result[2] << " " << result[1] << " " << result[0] << endl;
map<char, int,less<int>> A_{ {'B',B_A},{'C',C_A},{'J',J_A} };
map<char, int,less<int>> B_{ {'B',B_B},{'C',C_B},{'J',J_B} };
map<char, int, less<int>>::iterator it_a = A_.begin();
map<char, int, less<int>>::iterator it_b = A_.begin();
cout << it_a->first << " " << it_b->first ;
}
AC代码(参考 算法笔记):
#include <iostream>
#include <cstdio>
using namespace std;
int change(char c){
if(c=='B') return 0;
if(c=='C') return 1;
if(c=='J') return 2;
}
const int maxn=1000001;
int main(){
int N;
char mp[3]={'B','C','J'};
int time_[3]={0};//0:代表甲赢,1:代表平局,2:代表乙赢
int hard_A[3]={0},hard_B[3]={0};//分别代表甲,乙获胜次数最多的手势
scanf("%d",&N);
while(N--){
int k1,k2;
char c1,c2;
getchar();//由于scanf使用%c时会将换行符\n读入,因此需要在合适的地方用getchar吸收空格
//否则,会导致程序读入数据后闪退,
scanf("%c %c",&c1,&c2);
k1=change(c1);
k2=change(c2);
if((k1+1)%3==k2){//甲赢
time_[0]++;
hard_A[k1]++;
}
else if((k2+1)%3==k1){//乙赢
time_[2]++;
hard_B[k2]++;
}
else{
time_[1]++;
}
}
printf("%d %d %d\n",time_[0],time_[1],time_[2]);
printf("%d %d %d\n",time_[2],time_[1],time_[0]);
int max_a=0,max_b=0;
for(int i=1;i<3;i++){
if(hard_A[max_a]<hard_A[i])
max_a=i;
if(hard_B[max_b]<hard_B[i])
max_b=i;
}
printf("%c %c",mp[max_a],mp[max_b]);
return 0;
}