题目
input&&output
Sample
#input:
8
summon 1 3 6
summon 2 4 2
end
summon 1 4 5
summon 1 2 1
attack 1 2
end
attack 1 1
#output:
0
30
1 2
30
1 2
题解
1.本题代码采用了面向对象的方式,实际上直接写也可以
2.分析题目我们发现有三种操作,end summon attack
我们将随从看作一个结构体 {hp,attack}
每个人可以拥有7个随从,(关于玩家两种做法 1看作随从0 2另外看作一个变量 此处采用第一种)
end操作:结束回合
summon操作 安置随从,由于采用vector保存随从 因此直接采用insert操作放置
attack操作 攻击操作 攻击对应位置 有反伤 血量为负 可使用erase操作删除vector上的随从
由于题目中说明数据全部符合规范,因此我们无需担心越界等问题
3.由于双方互相进攻,因此我们采用一个中间变量对其进行一系列操作最后将其再赋给 当前玩家即可。
C++代码
#include<iostream>
#include<vector>
using namespace std;
int win;
struct attend{
int hp;
int attack;
};
class player{
public:
player(){
att = vector<attend>(0);
attend her;
her.attack=0,her.hp=30;
att.push_back(her);
}
void summon(attend& curr,int pos){
att.insert(att.begin()+pos,curr);
}
int gethp(){
return this->att[0].hp;
}
void attack(int att_pos,player& defender,int defe_pos){
defender.att[defe_pos].hp -= this->att[att_pos].attack;
if(defe_pos != 0){//攻击随从 有反伤或死翘翘
this->att[att_pos].hp -= defender.att[defe_pos].attack;
if(this->att[att_pos].hp<=0) this->att.erase(this->att.begin()+att_pos);
if(defender.att[defe_pos].hp<=0) defender.att.erase(defender.att.begin()+defe_pos);
}
}
void out(){
cout<<this->att[0].hp<<endl;
cout<<this->att.size()-1;
for(int i=1;i<att.size();i++) cout<<" "<<att[i].hp;
cout<<endl;
}
private:
vector<attend> att;
};
int main(){
int n,opre;
string ope;
cin>>n;
player A,B,now = A;
int now_p = 0;//0代表A
attend curr;
while(n--){
int a,b,c;
cin>>ope;
if(ope=="summon"){
cin>>a>>b>>c;
curr.hp = c;
curr.attack = b;
now.summon(curr,a);
}
if(ope=="attack"){
cin>>a>>b;
if(now_p == 0) now.attack(a,B,b);
else now.attack(a,A,b);
}
if(ope=="end"){
if(now_p==0){
A = now;
now_p = 1;
now = B;
}else{
B = now;
now_p = 0;
now = A;
}
}
}
if(now_p==0) A = now;
else B = now;
//最后一个没有end
if(B.gethp()<=0) win=1;
if(A.gethp()<=0) win=-1;
cout<<win<<endl;
A.out();
B.out();
return 0;
}