大家好啊,这里是Cyber Striver的博客。今天我继续为大家带来PAT乙级的题解。
目录
题目
题解
#include <stdio.h>
// B C J
int main()
{
int N ;
scanf("%d",&N);
getchar();
char a,b;
// 对于r数组,为0,1,2分别为甲赢,乙赢,平局
int output[3]={0};
// 甲方: 赢 平 负
// 0-2为甲,3-5为乙 以此为 布 锤子 剪刀
int count[6]={0};
// 读入数据
// 数据处理
// 布 锤子 剪刀
// B C J
for (int i = 0; i < N; i++)
{
scanf("%c %c",&a,&b);
getchar();
// 直接暴力枚举?一共有7种可能
if (a == b)
output[2]++;
else if (a == 'C')
if (b == 'J')
{
output[0]++;
count[1]++;
}
else
{
output[1]++;
count[3]++;
}
else if (a == 'J')
if (b == 'C')
{
output[1]++;
count[4]++;
}
else
{
output[0]++;
count[2]++;
}
else if (a == 'B')
if (b == 'C')
{
output[0]++;
count[0]++;
}
else
{
output[1]++;
count[5]++;
}
}
// 甲方: 赢 平 负
printf("%d %d %d\n",output[0],output[2],output[1]);
printf("%d %d %d\n",output[1],output[2],output[0]);
// 找到最大值的索引
char p[3] = {'B','C','J'};
int max_index1 = 0;
int max_index2 = 3;
for (int i = 0 ;i < 3; i++) {
if (count[i] > count[max_index1]) {
max_index1 = i;
}
}
for (int i = 3 ;i < 6; i++) {
if (count[i] > count[max_index2]) {
max_index2 = i-3;
}
}
if(max_index2==3)
max_index2-=3;
printf("%c %c",p[max_index1],p[max_index2]);
// 何不换一种思路用 B C J 的方式来写呢? 相同的话直接优先!
// C J B
return 0;
}
思路
解决段错误,一开始直接创了几个有一万多个元素的数组从而导致段错误。
经验总结
本次博客到这里就结束了,如果你觉得我的文章还不错的话,请给我多多点赞评论哦!