注意: 总时间限制: 1000ms 内存限制: 65536kB
魔兽世界的西面是红魔军的司令部,东面是蓝魔军的司令部。两个司令部之间是依次排列的若干城市。
红司令部,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分开始到双方司令部停止制造武士为止的所有事件按顺序输出。
一共有两种事件,其对应的输出样例如下:
武士降生
输出样例: 004 blue lion 5 born with strength 5,2 lion in red headquarter
表示在4点整,编号为5的蓝魔lion武士降生,它降生时生命值为5,降生后蓝魔司令部里共有2个lion武士。(为简单起见,不考虑单词的复数形式)注意,每制造出一个新的武士,都要输出此时司令部里共有多少个该种武士。
司令部停止制造武士
输出样例: 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
using namespace std;
class Dragon {
private:
int strength = 0;
public:
void set_strength(int strength_) {
strength = strength_;
}
int getStrength() {
return strength;
}
};
class Ninja {
private:
int strength = 0;
public:
void set_strength(int strength_) {
strength = strength_;
}
int getStrength() {
return strength;
}
};
class Iceman {
private:
int strength = 0;
public:
void set_strength(int strength_) {
strength = strength_;
}
int getStrength() {
return strength;
}
};
class Lion {
private:
int strength = 0;
public:
void set_strength(int strength_) {
strength = strength_;
}
int getStrength() {
return strength;
}
};
class Wolf {
private:
int num_red = 0, num_blue = 0;
int strength = 0;
public:
void set_strength(int strength_) {
strength = strength_;
}
int getStrength() {
return strength;
}
};
class Red {
private:
int dragonNum = 0, ninjaNum = 0, icemanNum = 0, lionNum = 0, wolfNum = 0;
int barrackStrength = 0;
char previousSamurai = '\0';
public:
void add_dragonNum() {
dragonNum++;
}
void add_ninjaNum() {
ninjaNum++;
}
void add_icemanNum() {
icemanNum++;
}
void add_lionNum() {
lionNum++;
}
void add_wolfNum() {
wolfNum++;
}
void set_initial_strength(int strength) {
barrackStrength = strength;
}
void cost_barrack_strength(int cost) {
barrackStrength -= cost;
}
void set_previous_samurai(char name) {
previousSamurai = name;
}
int get_dragonNum() {
return dragonNum;
}
int get_ninjaNum() {
return ninjaNum;
}
int get_icemanNum() {
return icemanNum;
}
int get_lionNum() {
return lionNum;
}
int get_wolfNum() {
return wolfNum;
}
int get_barrackStrength() {
return barrackStrength;
}
char get_previous_samurai() {
return previousSamurai;
}
int getSum() {
return dragonNum + ninjaNum + icemanNum + lionNum + wolfNum;
}
};
class Blue {
private:
int dragonNum = 0, ninjaNum = 0, icemanNum = 0, lionNum = 0, wolfNum = 0;
int barrackStrength = 0;
char previousSamurai = '\0';
public:
void add_dragonNum() {
dragonNum++;
}
void add_ninjaNum() {
ninjaNum++;
}
void add_icemanNum() {
icemanNum++;
}
void add_lionNum() {
lionNum++;
}
void add_wolfNum() {
wolfNum++;
}
void set_initial_strength(int strength) {
barrackStrength = strength;
}
void cost_barrack_strength(int cost) {
barrackStrength -= cost;
}
void set_previous_samurai(char name) {
previousSamurai = name;
}
int get_dragonNum() {
return dragonNum;
}
int get_ninjaNum() {
return ninjaNum;
}
int get_icemanNum() {
return icemanNum;
}
int get_lionNum() {
return lionNum;
}
int get_wolfNum() {
return wolfNum;
}
int get_barrackStrength() {
return barrackStrength;
}
char get_previous_samurai() {
return previousSamurai;
}
int getSum() {
return dragonNum + ninjaNum + icemanNum + lionNum + wolfNum;
}
};
class Genaration {
private:
int time = 0;
Red red;
Blue blue;
Dragon dragon;
Ninja ninja;
Iceman iceman;
Lion lion;
Wolf wolf;
int red_situation = 0; //变成1说明已经红色司令部停止制造武士
int blue_situation = 0; //变成1说明已经蓝色司令部停止制造武士
public:
void genaration() {
// cout << "red.get_previous_samurai() " << red.get_previous_samurai() << endl;
// cout << "get_barrackStrength() " << red.get_barrackStrength() << endl;
// cout << "dragon.getStrength() " << dragon.getStrength();
do {
if (red_situation == 1 && blue_situation == 1) {
exit(1);
} else {
genaration_red();
genaration_blue();
time++;
}
} while (true);
}
void genaration_red() {
if ((red.get_previous_samurai() == '\0' || red.get_previous_samurai() == 'd') &&
iceman.getStrength() <= red.get_barrackStrength()) {
red.add_icemanNum();
red.cost_barrack_strength(iceman.getStrength());
outline_red('i');
red.set_previous_samurai('i');
} else if (red.get_previous_samurai() == 'i' && lion.getStrength() <= red.get_barrackStrength()) {
red.add_lionNum();
red.cost_barrack_strength(lion.getStrength());
outline_red('l');
red.set_previous_samurai('l');
} else if (red.get_previous_samurai() == 'l' && wolf.getStrength() <= red.get_barrackStrength()) {
red.add_wolfNum();
red.cost_barrack_strength(wolf.getStrength());
outline_red('w');
red.set_previous_samurai('w');
} else if (red.get_previous_samurai() == 'w' && ninja.getStrength() <= red.get_barrackStrength()) {
red.add_ninjaNum();
red.cost_barrack_strength(ninja.getStrength());
outline_red('n');
red.set_previous_samurai('n');
} else if (red.get_previous_samurai() == 'n' && dragon.getStrength() <= red.get_barrackStrength()) {
red.add_dragonNum();
red.cost_barrack_strength(dragon.getStrength());
outline_red('d');
red.set_previous_samurai('d');
} else if (red_situation == 1) { ;
} else {
outline_stop_red();
red_situation = 1;
}
};
void genaration_blue() {
if ((blue.get_previous_samurai() == '\0' || blue.get_previous_samurai() == 'w') &&
lion.getStrength() <= blue.get_barrackStrength()) {
blue.add_lionNum();
blue.cost_barrack_strength(lion.getStrength());
outline_blue('l');
blue.set_previous_samurai('l');
} else if (blue.get_previous_samurai() == 'l' && dragon.getStrength() <= blue.get_barrackStrength()) {
blue.add_dragonNum();
blue.cost_barrack_strength(dragon.getStrength());
outline_blue('d');
blue.set_previous_samurai('d');
} else if (blue.get_previous_samurai() == 'd' && ninja.getStrength() <= blue.get_barrackStrength()) {
blue.add_ninjaNum();
blue.cost_barrack_strength(ninja.getStrength());
outline_blue('n');
blue.set_previous_samurai('n');
} else if (blue.get_previous_samurai() == 'n' && iceman.getStrength() <= blue.get_barrackStrength()) {
blue.add_icemanNum();
blue.cost_barrack_strength(iceman.getStrength());
outline_blue('i');
blue.set_previous_samurai('i');
} else if (blue.get_previous_samurai() == 'i' && wolf.getStrength() <= blue.get_barrackStrength()) {
blue.add_wolfNum();
blue.cost_barrack_strength(wolf.getStrength());
outline_blue('w');
blue.set_previous_samurai('w');
} else if (blue_situation == 1) { ;
} else {
outline_stop_blue();
blue_situation = 1;
}
};
void outline_red(char name) {
outTime();
cout << "red ";
outline_choose_red(name);
cout << endl;
}
void outline_blue(char name) {
outTime();
cout << "blue ";
outline_choose_blue(name);
cout << endl;
}
void outline_stop_red() {
outTime();
cout << "red headquarter stops making warriors" << endl;
}
void outline_stop_blue() {
outTime();
cout << "blue headquarter stops making warriors" << endl;
}
void outTime() {
if (time < 10)
cout << "00" << time << " ";
else if (time < 100)
cout << "0" << time << " ";
else
cout << time << " ";
}
void outline_choose_red(char name) {
if (name == 'd')
cout << "dragon " << red.getSum() << " " << "born with strength " << dragon.getStrength() << ","
<< red.get_dragonNum() << " dragon in red headquarter";
else if (name == 'n')
cout << "ninja " << red.getSum() << " " << "born with strength " << ninja.getStrength() << ","
<< red.get_ninjaNum() << " ninja in red headquarter";
else if (name == 'i')
cout << "iceman " << red.getSum() << " " << "born with strength " << iceman.getStrength() << ","
<< red.get_icemanNum() << " iceman in red headquarter";
else if (name == 'l')
cout << "lion " << red.getSum() << " " << "born with strength " << lion.getStrength() << ","
<< red.get_lionNum() << " lion in red headquarter";
else
cout << "wolf " << red.getSum() << " " << "born with strength " << wolf.getStrength() << ","
<< red.get_wolfNum() << " wolf in red headquarter";
}
void outline_choose_blue(char name) {
if (name == 'd')
cout << "dragon " << blue.getSum() << " " << "born with strength " << dragon.getStrength() << ","
<< blue.get_dragonNum() << " dragon in blue headquarter";
else if (name == 'n')
cout << "ninja " << blue.getSum() << " " << "born with strength " << ninja.getStrength() << ","
<< blue.get_ninjaNum() << " ninja in blue headquarter";
else if (name == 'i')
cout << "iceman " << blue.getSum() << " " << "born with strength " << iceman.getStrength() << ","
<< blue.get_icemanNum() << " iceman in blue headquarter";
else if (name == 'l')
cout << "lion " << blue.getSum() << " " << "born with strength " << lion.getStrength() << ","
<< blue.get_lionNum() << " lion in blue headquarter";
else
cout << "wolf " << blue.getSum() << " " << "born with strength " << wolf.getStrength() << ","
<< blue.get_wolfNum() << " wolf in blue headquarter";
}
void set_dragon_strength(int strength) {
dragon.set_strength(strength);
}
void set_ninja_strength(int strength) {
ninja.set_strength(strength);
}
void set_iceman_strength(int strength) {
iceman.set_strength(strength);
}
void set_lion_strength(int strength) {
lion.set_strength(strength);
}
void set_wolf_strength(int strength) {
wolf.set_strength(strength);
}
void set_initial_strength(int strength) {
red.set_initial_strength(strength);
blue.set_initial_strength(strength);
}
};
int main() {
Genaration test;
int testNum, dragonstrength, ninjastrength, icemanstrength, lionstrength, wolfstrength, barrackstrength;
cin >> testNum;
cin >> barrackstrength;
scanf("%d %d %d %d %d", &dragonstrength, &ninjastrength, &icemanstrength, &lionstrength, &wolfstrength);
test.set_dragon_strength(dragonstrength);
test.set_ninja_strength(ninjastrength);
test.set_iceman_strength(icemanstrength);
test.set_lion_strength(lionstrength);
test.set_wolf_strength(wolfstrength);
test.set_initial_strength(barrackstrength);
cout << "Case:" << testNum << endl;
test.genaration();
}
看了郭老师给出的代码之后,在其基础上重新写了一次。代码量明显小了不少:
#include
#include
#include
using namespace std;
const int WARRIOR_NUM = 5;
/*
string Warrior::names[WARRIOR_NUM] = { "dragon","ninja","iceman","lion","wolf" };
红方司令部按照 iceman、lion、wolf、ninja、dragon 的顺序制造武士。
蓝方司令部按照 lion、dragon、ninja、iceman、wolf 的顺序制造武士。
*/
class Headquarter;
class Warrior
{
private:
Headquarter * pHeadquarter;
int kindNo; //武士的种类编号 0 dragon 1 ninja 2 iceman 3 lion 4 wolf
int no;
public:
static string names[WARRIOR_NUM];
static int initialLifeValue [WARRIOR_NUM];
Warrior( Headquarter * p,int no_,int kindNo_ );
void PrintResult(int nTime);
};
class Headquarter
{
private:
int totalLifeValue;
bool stopped;
int totalWarriorNum;
int color;
int curMakingSeqIdx; //当前要制造的武士是制造序列中的第几个
int warriorNum[WARRIOR_NUM]; //存放每种武士的数量
Warrior * pWarriors[1000];
public:
friend class Warrior;
static int makingSeq[2][WARRIOR_NUM]; //武士的制作顺序序列
void Init(int color_, int lv);
~Headquarter () ;
int Produce(int nTime);
string GetColor();
};
Warrior::Warrior( Headquarter * p,int no_,int kindNo_ ) {
no = no_;
kindNo = kindNo_;
pHeadquarter = p;
}
void Warrior::PrintResult(int nTime)
{
string color = pHeadquarter->GetColor();
printf("%03d %s %s %d born with strength %d,%d %s in %s headquarter\n" ,
nTime, color.c_str(), names[kindNo].c_str(), no, initialLifeValue[kindNo],
pHeadquarter->warriorNum[kindNo],names[kindNo].c_str(),color.c_str());
}
void Headquarter::Init(int color_, int lv)
{
color = color_;
totalLifeValue = lv;
totalWarriorNum = 0;
stopped = false;
curMakingSeqIdx = 0;
for( int i = 0;i < WARRIOR_NUM;i++ )
warriorNum[i] = 0;
}
Headquarter::~Headquarter () {
for( int i = 0;i < totalWarriorNum;i++ )
delete pWarriors[i];
}
int Headquarter::Produce(int nTime)
{
if( stopped )
return 0;
int searchingTimes = 0;
while( Warrior::initialLifeValue[makingSeq[color][curMakingSeqIdx]] > totalLifeValue &&
searchingTimes < WARRIOR_NUM ) {
curMakingSeqIdx = ( curMakingSeqIdx + 1 ) % WARRIOR_NUM;
searchingTimes++;
}
int kindNo = makingSeq[color][curMakingSeqIdx];
if( Warrior::initialLifeValue[kindNo] > totalLifeValue ) {
stopped = true;
if( color == 0)
printf("%03d red headquarter stops making warriors\n",nTime);
else
printf("%03d blue headquarter stops making warriors\n",nTime);
return 0;
}
//制作士兵:
totalLifeValue -= Warrior::initialLifeValue[kindNo];
curMakingSeqIdx = ( curMakingSeqIdx + 1 ) % WARRIOR_NUM;
pWarriors[totalWarriorNum] = new Warrior( this,totalWarriorNum+1,kindNo);
warriorNum[kindNo]++;
pWarriors[totalWarriorNum]->PrintResult(nTime);
totalWarriorNum++;
return 1;
}
string Headquarter::GetColor()
{
if( color == 0)
return "red";
else
return "blue";
}
string Warrior::names[WARRIOR_NUM] = { "dragon","ninja","iceman","lion","wolf" };
int Warrior::initialLifeValue [WARRIOR_NUM];
int Headquarter::makingSeq[2][WARRIOR_NUM] = { { 2,3,4,1,0 },{3,0,1,2,4} }; //两个司令部武士的制作顺序序列
int main()
{
int t;
int m;
Headquarter RedHead,BlueHead;
scanf("%d",&t);
int nCaseNo = 1;
while ( t-- ) {
printf("Case:%d\n",nCaseNo++);
scanf("%d",&m);
for( int i = 0;i < WARRIOR_NUM;i++ )
scanf("%d", & Warrior::initialLifeValue[i]);
RedHead.Init(0,m);
BlueHead.Init(1,m);
int nTime = 0;
while(true) {
int tmp1 = RedHead.Produce(nTime);
int tmp2 = BlueHead.Produce(nTime);
if( tmp1 == 0 && tmp2 == 0)
break;
nTime++;
}
}
return 0;
}