炉石传说

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

解题思路

  • 用结构体存储随从,vector存储双方的所有随从
  • 每次根据输入对应操作即可,插入时直接调用vector的insert,插到指定位置即可。攻击时两方对应减少健康值,生命值小于0则从vector中根据索引删掉。
  • 本题难度不大,只是复杂些

完整代码

#include<iostream>
#include<cstring>
#include<vector>
using namespace std;

struct suicong
{
int attack;
int health;
};

string s;
vector<suicong> a;
vector<suicong> b;
int N;
int now=0;//0代表a,1代表b 
int from,to,pos;


void attack()
{
cin>>from>>to;
if(now==0)//a的回合
{a[from].health=a[from].health-b[to].attack;
b[to].health=b[to].health-a[from].attack;
if(a[from].health<=0)
	a.erase(a.begin()+from);
if(b[to].health<=0)
	b.erase(b.begin()+to);}
else//b的回合 
{b[from].health=b[from].health-a[to].attack;
a[to].health=a[to].health-b[from].attack; 
if(b[from].health<=0)
	b.erase(b.begin()+from);  
if(a[to].health<=0)
	a.erase(a.begin()+to);
}
}

int summon()
{
if(now==0)//a的回合
{	cin>>pos;
	suicong mid;
	cin>>mid.attack>>mid.health;
	a.insert(a.begin()+pos,mid);	

}
else
{	cin>>pos;
	suicong mid;
	cin>>mid.attack>>mid.health;
	b.insert(b.begin()+pos,mid);
	
}

} 
int main()
{
cin>>N;

suicong abegin;
abegin.health=30;
abegin.attack=0;
suicong bbegin;
bbegin.health=30;
bbegin.attack=0;
a.insert(a.begin(),abegin);
b.insert(b.begin(),bbegin);

while(N--)
{
cin>>s;
if(s=="summon")
	summon();
else if(s=="attack")
	attack();
else if(s=="end")
	if(now==0)
		now=1;
	else
		now=0;	

}
if(a[0].health>0&&b[0].health<0)
	cout<<1<<endl;
else if(b[0].health>0&&a[0].health<0)
	cout<<-1<<endl;
else
	cout<<0<<endl;

cout<<a[0].health<<endl;
if(a[0].health>0)
	cout<<a.size()-1<<" ";
else
	cout<<a.size()<<" ";
	
for(int i=1;i<a.size();i++)
	cout<<a[i].health<<" ";
cout<<endl;
cout<<b[0].health<<endl;
if(b[0].health>0)
	cout<<b.size()-1<<" ";
else
	cout<<b.size()<<" ";

for(int i=1;i<b.size();i++)
	cout<<b[i].health<<" ";
cout<<endl;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值