开始是用python写的,结果最后一个超时,而代码已经很简单了,应该是边编译边运行的原因,pat可能检测的是运行时间,之后用c++写(先编译后运行),果然可以了,耗时很短。
题目:
输入样例:
10
C J
J B
C B
B B
B C
C C
C B
J B
B C
J J
输出样例:
5 3 2
2 3 5
B B
代码(看着挺长,其实挺简单的):
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int max1, max2; // 甲,乙获胜次数最多的手势的下标
int win = 0, tie, lose = 0; //甲的比赛情况,乙的和他相反
int w1[3] = {0, 0, 0}, w2[3] = {0, 0, 0}; //记录甲,乙获胜手势的次数
int n;
cin >> n;
for (int i = 0; i < n; i += 1)
{
char x, y;
cin >> x >> y;
if (x == 'B' && y == 'C')
{
win += 1;
w1[0] += 1;
}
else if (x == 'B' && y == 'J')
{
lose += 1;
w2[2] += 1;
}
else if (x == 'C' && y == 'J')
{
win += 1;
w1[1] += 1;
}
else if (x == 'C' && y == 'B')
{
lose += 1;
w2[0] += 1;
}
else if (x == 'J' && y == 'B')
{
win += 1;
w1[2] += 1;
}
else if (x == 'J' && y == 'C')
{
lose += 1;
w2[1] += 1;
}
}
tie = n - lose - win;//平局次数
cout << win << " " << tie << " " << lose << endl;
cout << lose << " " << tie << " " << win << endl;
max1 = max_element(w1, w1 + 3) - w1;
max2 = max_element(w2, w2 + 3) - w2;
if (max1 == 0) //输出甲获胜最多的手势
cout << 'B' << ' ';
else if (max1 == 1)
cout << 'C' << ' ';
else
cout << 'J' << ' ';
if (max2 == 0) //输出已获胜最多的手势
cout << 'B';
else if (max2 == 1)
cout << 'C';
else
cout << 'J';
return 0;
}
来源:CSDN
作者:Francois4399
链接:https://blog.csdn.net/chongchujianghu3/article/details/103751616