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 CSP 201609-3 炉石传说(Java-100分)
- CCF CSP试题 201609-3 炉石传说
- ccf 201609-3 炉石传说
- CCF 201609-3 炉石传说 (100分)
- 201609-3 炉石传说 ccf
- CCF 201609-3 炉石传说
- CCF 201609-3 炉石传说
- 炉石传说 ccf 201609_3试题
- CCF CSP 201609-3:炉石传说
- CCF 201609-3 炉石传说 解题报告 模拟
- [CCFwithAxin]201609-3炉石传说(模拟题)
- CCF CSP 编程题目和解答-----试题名称:炉石传说 -------201609-3
- CCF考试——201609-3炉石传说
- CCF 201609-3 炉石传说
- CCF CSP 201609-3 炉石传说
- csp ccf 201609-3 第三题炉石传说java 100
- CCF CSP试题 201609-2 火车购票
- CCF 交通规划 201609-4(dijkstra)
- ccf csp 炉石传说 Java
- 201609-3炉石传说