直接贴代码:
#include <vector> #include <iostream> #include <iomanip> #include <string> using namespace std; const char TIME_FILL = '0'; const int TIME_W = 3; const int WAR_NUM = 5; class Warrior { public: Warrior() { }; Warrior(int hp_, int id_) : hp(hp_), id(id_) { }; private: int hp; int id; }; class Lion : public Warrior { public: Lion() { }; Lion(int hp_, int id_, int loy_) : Warrior(hp_, id_), loyalty(loy_) { }; private: int loyalty; }; class Ninja : public Warrior { public: Ninja() { }; Ninja(int hp_, int id_) : Warrior(hp_, id_) { }; void* weapon_1; void* weapon_2; private: }; class Dragon : public Warrior { public: Dragon() { }; Dragon(int hp_, int id_, double morale_) : Warrior(hp_, id_), morale(morale_) { }; void* weapon; private: double morale; }; class Iceman : public Warrior { public: Iceman() { }; Iceman(int hp_, int id_) : Warrior(hp_, id_) { }; void* weapon; private: }; class Wolf : public Warrior { public: Wolf() { }; Wolf(int hp_, int id_) : Warrior(hp_, id_) { }; }; class Sword { public: Sword() { }; }; class Bomb { public: Bomb() { }; }; class Arrow { public: Arrow() { }; }; class GameTime { public: GameTime(); bool Tick(); bool Disp(); bool Init(); private: unsigned int nTime; }Clock; class HeadQuarter { public: HeadQuarter(string, int); bool ProWar(); unsigned int nElement; string strFaction; bool bProEnd; private: int nFaction; int nMark; int nNinja; int nIceman; int nWolf; int nLion; int nDragon; int nWarrior; bool ProMark(int); bool ProIceman(); bool ProNinja(); bool ProDragon(); bool ProLion(); bool ProWolf(); vector<Dragon> vDragon; vector<Lion> vLion; vector<Ninja> vNinja; vector<Iceman> vIceman; vector<Wolf> vWolf; }; unsigned int dHP, nHP, iHP, lHP, wHP; int main() { int nTest; cin >> nTest; for(int test = 1; test <= nTest; test++) { Clock.Init(); int nElement; cin >> nElement; cin >> dHP >> nHP >> iHP >> lHP >> wHP; HeadQuarter red("red", nElement); HeadQuarter blue("blue", nElement); cout << "Case:" << test << endl; while(true) { if(red.bProEnd && blue.bProEnd) { break; } if(!red.bProEnd) { red.ProWar(); } if(!blue.bProEnd) { blue.ProWar(); } Clock.Tick(); } } return 0; } GameTime::GameTime() { nTime = 0; } bool GameTime::Tick() { nTime++; return true; } bool GameTime::Disp() { cout << setfill(TIME_FILL) << setw(TIME_W) << nTime; return true; } bool GameTime::Init() { nTime = 0; return true; } HeadQuarter::HeadQuarter(string faction_, int element_) { strFaction = faction_; nElement = element_; nMark = 0; bProEnd = false; nDragon = 0; nNinja = 0; nIceman = 0; nLion = 0; nWolf = 0; nWarrior = 0; } bool HeadQuarter::ProWar() { int i; bool bProFlag = false; for(i = nMark; i < WAR_NUM; ++i) { bProFlag = ProMark(i); if(bProFlag) { nMark = i + 1; break; } } if(!bProFlag) { for(i = 0; i < nMark; i++) { bProFlag = ProMark(i); if(bProFlag) { nMark = i + 1; break; } } } if(!bProFlag) { bProEnd = true; Clock.Disp(); cout << " " << strFaction << " headquarter stops making warriors" << endl; } else { if(nMark >= 5) { nMark = 0; } } return true; } bool HeadQuarter::ProMark(int mark_) { if(strFaction == "red") { switch(mark_) { case 0: return ProIceman(); case 1: return ProLion(); case 2: return ProWolf(); case 3: return ProNinja(); case 4: return ProDragon(); default: return false; } } if(strFaction == "blue") { switch(mark_) { case 0: return ProLion(); case 1: return ProDragon(); case 2: return ProNinja(); case 3: return ProIceman(); case 4: return ProWolf(); default: return false; } } return false; } bool HeadQuarter::ProIceman() { if(nElement >= iHP) { nWarrior++; nIceman++; nElement -= iHP; Clock.Disp(); cout << " " << strFaction << " iceman " << nWarrior << " born with strength " << iHP << "," << nIceman << " iceman in " << strFaction << " headquarter" << endl; cout << "It has a "; Iceman tmp(iHP, nWarrior); switch(nWarrior % 3) { case 0: tmp.weapon = new Sword; cout << "sword" << endl; break; case 1: tmp.weapon = new Bomb; cout << "bomb" << endl; break; case 2: tmp.weapon = new Arrow; cout << "arrow" << endl; break; } vIceman.push_back(tmp); return true; } else return false; } bool HeadQuarter::ProDragon() { if(nElement >= dHP) { nWarrior++; nDragon++; nElement -= dHP; Clock.Disp(); cout << " " << strFaction << " dragon " << nWarrior << " born with strength " << dHP << "," << nDragon << " dragon in " << strFaction << " headquarter" << endl; Dragon tmp(dHP, nWarrior, ((double)nElement / (double)dHP)); cout << "It has a "; switch(nWarrior % 3) { case 0: tmp.weapon = new Sword; cout << "sword"; break; case 1: tmp.weapon = new Bomb; cout << "bomb"; break; case 2: tmp.weapon = new Arrow; cout << "arrow"; break; } cout.setf(ios::fixed | ios::showpoint); cout.precision(2); cout << ",and it's morale is " << ((double)nElement / (double)dHP) << endl; vDragon.push_back(tmp); return true; } else return false; } bool HeadQuarter::ProNinja() { if(nElement >= nHP) { nWarrior++; nNinja++; nElement -= nHP; Clock.Disp(); cout << " " << strFaction << " ninja " << nWarrior << " born with strength " << nHP << "," << nNinja << " ninja in " << strFaction << " headquarter" << endl; Ninja tmp(nHP, nWarrior); cout << "It has a "; switch(nWarrior % 3) { case 0: tmp.weapon_1 = new Sword; cout << "sword"; break; case 1: tmp.weapon_1 = new Bomb; cout << "bomb"; break; case 2: tmp.weapon_1 = new Arrow; cout << "arrow"; break; } cout << " and a "; switch((nWarrior + 1) % 3) { case 0: tmp.weapon_2 = new Sword; cout << "sword" << endl; break; case 1: tmp.weapon_2 = new Bomb; cout << "bomb" << endl; break; case 2: tmp.weapon_2 = new Arrow; cout << "arrow" << endl; break; } vNinja.push_back(tmp); return true; } else return false; } bool HeadQuarter::ProLion() { if(nElement >= lHP) { nWarrior++; nLion++; nElement -= lHP; Clock.Disp(); cout << " " << strFaction << " lion " << nWarrior << " born with strength " << lHP << "," << nLion << " lion in " << strFaction << " headquarter" << endl; Lion tmp(lHP, nWarrior, nElement); cout << "It's loyalty is " << nElement << endl; vLion.push_back(tmp); return true; } else return false; } bool HeadQuarter::ProWolf() { if(nElement >= wHP) { nWarrior++; nWolf++; nElement -= wHP; Clock.Disp(); cout << " " << strFaction << " wolf " << nWarrior << " born with strength " << wHP << "," << nWolf << " wolf in " << strFaction << " headquarter" << endl; Wolf tmp(wHP, nWarrior); vWolf.push_back(tmp); return true; } else return false; }
http://cxsjsx.openjudge.cn/warcraft201202s/A/
魔兽世界的西面是红魔军的司令部,东面是蓝魔军的司令部。两个司令部之间是依次排列的若干城市。
红司令部,City 1,City 2,……,City n,蓝司令部
两军的司令部都会制造武士。武士一共有 dragon 、ninja、iceman、lion、wolf 五种。每种武士都有编号、生命值这两种属性。
有的武士可以拥有武器。武器有三种,sword, bomb,和arrow,编号分别为0,1,2。
双方的武士编号都是从1开始计算。红方制造出来的第 n 个武士,编号就是n。同样,蓝方制造出来的第 n 个武士,编号也是n。
不同的武士有不同的特点。
dragon 可以拥有一件武器。编号为n的dragon降生时即获得编号为 n%3 的武器。dragon还有“士气”这个属性,是个浮点数,其值为它降生后其司令部剩余生命元的数量除以造dragon所需的生命元数量。
ninjia可以拥有两件武器。编号为n的ninjia降生时即获得编号为 n%3 和 (n+1)%3的武器。
iceman有一件武器。编号为n的iceman降生时即获得编号为 n%3 的武器。
lion 有“忠诚度”这个属性,其值等于它降生后其司令部剩余生命元的数目。
wolf没特点。
请注意,在以后的题目里,武士的士气,生命值,忠诚度在其生存期间都可能发生变化,都有作用,武士手中的武器随着使用攻击力也会发生变化。
武士在刚降生的时候有一个生命值。
在每个整点,双方的司令部中各有一个武士降生。
红方司令部按照 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武士。(为简单起见,不考虑单词的复数形式)注意,每制造出一个新的武士,都要输出此时司令部里共有多少个该种武士。
如果造出的是dragon,那么还要输出一行,例:
It has a arrow,and it's morale is 23.34
表示该dragon降生时得到了arrow,其士气是23.34(为简单起见,本题中arrow前面的冠词用a,不用an,士气精确到小数点后面2位,四舍五入)
如果造出的是ninjia,那么还要输出一行,例:
It has a bomb and a arrow
表示该ninjia降生时得到了bomb和arrow。
如果造出的是iceman,那么还要输出一行,例:
It has a sword
表示该iceman降生时得到了sword。
如果造出的是lion,那么还要输出一行,例:
It's loyalty is 24
表示该lion降生时的忠诚度是24。
2) 司令部停止制造武士
输出样例: 010 red headquarter stops making warriors
表示在 10点整,红方司令部停止制造武士
输出事件时:
首先按时间顺序输出;
同一时间发生的事件,先输出红司令部的,再输出蓝司令部的。