您的位置:首页 > 其它

CCF 201609-3 炉石传说

2017-09-16 13:10 369 查看
得了90分,提交时注意把freopen注释掉,当结构体的元素中有指针或数组时,使用“=”进行两个结构的简单赋值有可能出问题,每次添加或删除随从时记得更新follower_num。


#include<iostream>
#include<string>
const int max_follower_num = 7;
const int default_hero_health = 30;
using namespace std;

struct Follower
{
int attack, health;
Follower(int _attack=0, int _health=0):attack(_attack), health(_health){}
};
struct Hero
{
int id;
int health;
int follower_num;
Follower followers[max_follower_num+5];
Hero():health(default_hero_health), follower_num(0){}
void summon(int position, int attack, int health)
{
if(position == follower_num+1)
{
followers[position].attack = attack;
followers[position].health = health;
follower_num++;
}
else
{
for(int i = follower_num; i >= position; i--)
{
followers[i+1] = followers[i];
}
followers[position].attack = attack;
followers[position].health = health;
follower_num++;
}
/*
cout << "summon\n";
cout << "id = " << id << endl;
cout << "follower_num=" <<follower_num<<"\n";
for(int i = 1; i <= follower_num; i++)
cout << " " << followers[i].health;
cout << endl;
*/
}
void attack(int attacker, int defender, Hero& o_hero)
{
if(defender==0)
{
o_hero.health -= followers[attacker].attack;
}
else
{
followers[attacker].health -= o_hero.followers[defender].attack;
o_hero.followers[defender].health -= followers[attacker].attack;
if(followers[attacker].health <= 0)
{
for(int i = attacker; i < follower_num; i++)
{
followers[i] = followers[i+1];
}
follower_num--;
}
if(o_hero.followers[defender].health <= 0)
{
for(int i = defender; i < o_hero.follower_num; i++)
{
o_hero.followers[i] = o_hero.followers[i+1];
}
o_hero.follower_num--;
}
}

}
};

void show_outcome(Hero& hero1, Hero& hero2)
{

if(hero1.health < 0 && hero2.health > 0)
{
cout << "-1\n";
}else if(hero1.health > 0 && hero2.health < 0)
{
cout << "1\n";
}else if(hero1.health > 0 && hero2.health > 0)
{
cout << "0\n";
}
//  cout << "id = "<< hero1.id << endl;
cout << hero1.health << endl;
cout << hero1.follower_num;
for(int i = 1; i <= hero1.follower_num; i++)
{
cout << " " << hero1.followers[i].health;
}
cout << endl;

//  cout << "id = " << hero2.id << endl;
cout << hero2.health << endl;
cout << hero2.follower_num;
for(int i = 1; i <= hero2.follower_num; i++)
{
cout <<" "<< hero2.followers[i].health;
}
cout << endl;

}

int main()
{
//  freopen("input.txt", "r", stdin);
//  freopen("output.txt", "w", stdout);
Hero hero1, hero2;

hero1.id = 1;
hero2.id = 2;

int t, flag = 1;
cin >> t;
string action;
while(t--)
{

cin >> action;
if(action[0] == 's')
{
int position, attack, health;
cin >> position >> attack >> health;
if(flag == 1)
hero1.summon(position, attack, health);
else
hero2.summon(position, attack, health);

}
else if(action[0] == 'a')
{
int attacker, defender;
cin >> attacker >> defender;
if(flag == 1 )
hero1.attack(attacker, defender, hero2);
else
hero2.attack(attacker, defender, hero1);

}
else if(action[0] == 'e')
{
flag = flag==1?2:1;
}
}//while(t--)
show_outcome(hero1, hero2);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ccf 模拟