Skip to content

Instantly share code, notes, and snippets.

@techlarry
Created November 12, 2017 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save techlarry/27c71617ba7f988e726ce5c0ddc95b6f to your computer and use it in GitHub Desktop.
Save techlarry/27c71617ba7f988e726ce5c0ddc95b6f to your computer and use it in GitHub Desktop.
描述
魔兽世界的西面是红魔军的司令部,东面是蓝魔军的司令部。两个司令部之间是依次排列的若干城市。
红司令部,City 1,City 2,……,City n,蓝司令部
两军的司令部都会制造武士。武士一共有 dragon 、ninja、iceman、lion、wolf 五种。每种武士都有编号、生命值、攻击力这三种属性。
双方的武士编号都是从1开始计算。红方制造出来的第n个武士,编号就是n。同样,蓝方制造出来的第n个武士,编号也是n。
武士在刚降生的时候有一个生命值。
在每个整点,双方的司令部中各有一个武士降生。
红方司令部按照iceman、lion、wolf、ninja、dragon的顺序循环制造武士。
蓝方司令部按照lion、dragon、ninja、iceman、wolf的顺序循环制造武士。
制造武士需要生命元。
制造一个初始生命值为m的武士,司令部中的生命元就要减少m个。
如果司令部中的生命元不足以制造某个按顺序应该制造的武士,那么司令部就试图制造下一个。如果所有武士都不能制造了,则司令部停止制造武士。
给定一个时间,和双方司令部的初始生命元数目,要求你将从0点0分开始到双方司令部停止制造武士为止的所有事件按顺序输出。
一共有两种事件,其对应的输出样例如下:
1) 武士降生
输出样例: 004 blue lion 5 born with strength 5,2 lion in red headquarter
表示在4点整,编号为5的蓝魔lion武士降生,它降生时生命值为5,降生后蓝魔司令部里共有2个lion武士。(为简单起见,不考虑单词的复数形式)注意,每制造出一个新的武士,都要输出此时司令部里共有多少个该种武士。
2) 司令部停止制造武士
输出样例: 010 red headquarter stops making warriors
表示在10点整,红方司令部停止制造武士
输出事件时:
首先按时间顺序输出;
同一时间发生的事件,先输出红司令部的,再输出蓝司令部的。
输入
第一行是一个整数,代表测试数据组数。
每组测试数据共两行。
第一行:一个整数M。其含义为, 每个司令部一开始都有M个生命元( 1 <= M <= 10000)。
第二行:五个整数,依次是 dragon 、ninja、iceman、lion、wolf 的初始生命值。它们都大于0小于等于10000。
输出
对每组测试数据,要求输出从0时0分开始,到双方司令部都停止制造武士为止的所有事件。
对每组测试数据,首先输出"Case:n" n是测试数据的编号,从1开始 。
接下来按恰当的顺序和格式输出所有事件。每个事件都以事件发生的时间开头,时间以小时为单位,有三位。
样例输入
```
1
20
3 4 5 6 7
```
样例输出
```
Case:1
000 red iceman 1 born with strength 5,1 iceman in red headquarter
000 blue lion 1 born with strength 6,1 lion in blue headquarter
001 red lion 2 born with strength 6,1 lion in red headquarter
001 blue dragon 2 born with strength 3,1 dragon in blue headquarter
002 red wolf 3 born with strength 7,1 wolf in red headquarter
002 blue ninja 3 born with strength 4,1 ninja in blue headquarter
003 red headquarter stops making warriors
003 blue iceman 4 born with strength 5,1 iceman in blue headquarter
004 blue headquarter stops making warriors
```
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;
//武士
class knight
{
public:
knight(int b, int i, int d=0):blood(b), id(i), damage(d){}
private:
int blood; //生命值
int id; //编号
int damage;//攻击力
};
//司令部
class Command
{
public:
//constructor
Command(int _n, int _dragon, int _ninja, int _iceman, int _lion, int _wolf, string _flag):
n(_n), dragon(_dragon), ninja(_ninja), iceman(_iceman), lion(_lion), wolf(_wolf), flag(_flag), name(5), blood(5), num(5,0), id(1), producing_id(0){choose();}
bool produce();
void choose();
void stop_produce(); //停止生产
~Command();
private:
int lion, dragon, ninja, iceman, wolf; //iceman、lion、wolf、ninja、dragon 的初始生命值
knight * kni[100]; // 拥有的武士
int n; //生命元
int time; //时间
string flag; //红方或者蓝方
// 制造顺序
vector<string> name; //每一种武士名字
vector<int> blood; //每一种武士初始生命元
vector<int> num; //每一种武士数量
int id; //制造的武士数量:从1开始编号
int producing_id; //正在制造的武士编号:0-4;
};
Command::~Command()
{
for (int i=0;i<id;i++)
delete kni[i];
}
void Command::choose()
{
if (flag == string("red"))
{
vector<string> red_order_name{"iceman", "lion", "wolf", "ninja", "dragon"};
name = red_order_name;
vector<int> red_order_blood{iceman, lion, wolf, ninja, dragon};
blood = red_order_blood;
}
else if (flag == string("blue"))
{
vector<string> blue_order_name{"lion", "dragon", "ninja", "iceman", "wolf"};
name = blue_order_name;
vector<int> blue_order_blood{lion, dragon, ninja, iceman, wolf};
blood = blue_order_blood;
}
}
void Command::stop_produce()
{
cout << setw(3) << setfill('0') << time << " "; //输出时间: e.g. 004
cout << flag << " headquarter stops making warriors" << endl;
}
bool Command::produce()
{
//找到适合生产的武士
int j = 0;
while (blood[producing_id] > n)
{
j++;
producing_id = (producing_id+1)%5; //循环制造武士
if (j==5) //所有的都不能生产
{
stop_produce();
return false;
}
}
//生产武士
int i = producing_id;
kni[id] = new knight(blood[i], id);
//输出生产信息
string space = " ";
cout << setw(3) << setfill('0') << time << space; //输出时间: e.g. 004
cout << flag << space << name[i] << space << id; //输出武士: blue lion 5
cout << " born with strength " << blood[i] << "," ; //输出生命值:e.g. born with strength 5,
cout << num[i]+1 << space << name[i] << " in " << flag << " headquarter" << endl;
//更新信息
n -= blood[i]; //生命元减小
time += 1; //时间增加
num[i] = num[i]+1; //该种武士数量增加
id += 1; //编号增加
producing_id = (producing_id+1)%5; //循环制造武士
return true;
}
int main()
{
int num; //代表测试数据组数
int n; // //生命元
int b_dragon, b_ninja, b_iceman,b_lion, b_wolf; //依次是 dragon 、ninja、iceman、lion、wolf 的初始生命值
cin >> num;
cin >> n >> b_dragon >> b_ninja >> b_iceman >> b_lion >> b_wolf; //输入
Command blue(n, b_dragon, b_ninja, b_iceman,b_lion, b_wolf, string("blue"));
Command red(n, b_dragon, b_ninja, b_iceman,b_lion, b_wolf, string("red"));
bool p1=true, p2=true;
while( p1 || p2)
{
if (p2)
p2 = red.produce();
if (p1)
p1 = blue.produce();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment