csp2016-09-3_炉石传说

该博客详细介绍了CSP2016-09-3炉石传说问题的解题思路和代码实现。博主通过定义card结构体表示随从的血量和攻击力,使用vector创建二维数组player来存储两个玩家及其随从信息。主要内容包括召唤随从(使用vector的insert函数)和随从攻击(涉及英雄和随从之间的生命值变化及随从死亡判断,利用vector的erase函数移除死亡随从)。

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

csp2016-09-3_炉石传说


题目描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

题目思路

在这个题目里我定义了一个card结构体类型,其中有血量health和攻击力attack。然后定义了一个二维数组player,这里我使用的是vector定义的二维数组,这样的话在后面插入和删除的时候写起来会方便很多。player存储两个玩家和他们随从的血量和攻击力。在这个题目中涉及到的主要操作有召唤随从summon,随从攻击attack两大部分。其中召唤随从summon我们只需要使用vector中的insert函数即可实现。attack部分中涉及到是攻击英雄还是随从的问题。根据题目意思攻击方和被攻击方都会受到生命值的减少,这里我们使用求余的方法确定被攻击方,然后更新对应英雄或者随从的health,如果health小于等于0并且是随从的话,我们就需要将这个随从删除,这里也是使用vector中的erase函数。最后按要求输出即可。

代码实现

#include <iostream>
#include <vector> 
#include <cstdio>
#include <string>
using namespace std;
#define _for(i,a,b) for(int i = (a); i < b; i++)
#define _rep(i,a,b) for(int i = (a); i <= b; i++)

int n;
int flag;
string s;

struct card{
	int health;
	int attack;
	
	card(int h,int a):health(h),attack(a){}
};
vector<card> player[2];

void summon(int index,int position,int attack,int health){
	player[index].insert(player[index].begin()+position,card(health,attack));
}

void attack(int index1,int attacker,int defender){
	int index2 = (index1 + 1) % 2;
	player[index1][attacker].health	-= player[index2][defender].attack;
	player[index2][defender].health -= player[index1][attacker].attack;
	
	if(player[index1][attacker].health <= 0 && attacker != 0){
		player[index1].erase(player[index1].begin()+attacker);
	}
	if(player[index2][defender].health <= 0 && defender != 0){
		player[index2].erase(player[index2].begin()+defender);
	}
}

int main(){
	cin >> n;
	player[0].push_back(card(30,0));
	player[1].push_back(card(30,0));
	flag = 0;
	_for(i,0,n){
		cin >> s;
		if(s == "summon"){
			int pos,a,h;
			cin >> pos >> a >> h;
			summon(flag,pos,a,h);
		}
		else if(s == "attack"){
			int a,d;
			cin >> a >> d;
			attack(flag,a,d);
		}
		else{
			flag = (flag + 1) % 2;
		}
	}
	if(player[0][0].health <= 0){
		cout << -1 << endl;
	}
	else if(player[1][0].health <= 0){
		cout << 1 << endl;
	}
	else{
		cout << 0 << endl;
	}
	_for(i,0,2){
		cout << player[i][0].health << endl << player[i].size() - 1 << " ";
		_for(j,1,player[i].size()){
			cout << player[i][j].health << " ";
		}
		cout << endl;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值