PAT Basic Level 1018 锤子剪刀布 (20 分)

本文探讨了使用C++实现策略游戏中的博弈算法,通过分析玩家的手势选择,计算比赛结果,并找出玩家最常用的手势。该算法适用于石头、剪刀、布等策略游戏,展示了如何高效地处理大量游戏回合,以及如何利用map和条件判断来优化代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值