欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

炉石传说

程序员文章站 2022-03-20 08:22:41
...

炉石传说
炉石传说
炉石传说
炉石传说

解题思路

  • 用结构体存储随从,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;

}
相关标签: 实验