POJ C++程序设计 编程题#2 魔兽世界之二:装备

编程题#2: 魔兽世界之二:装备

来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

注意: 总时间限制: 1000ms 内存限制: 65536kB

描述

魔兽世界的西面是红魔军的司令部,东面是蓝魔军的司令部。两个司令部之间是依次排列的若干城市。

红司令部,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点整,红方司令部停止制造武士

输出事件时:

首先按时间顺序输出;

同一时间发生的事件,先输出红司令部的,再输出蓝司令部的。

 

输入

第一行是一个整数,代表测试数据组数。

每组测试数据共两行。

第一行,一个整数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
It has a bomb
000 blue lion 1 born with strength 6,1 lion in blue headquarter
It's loyalty is 14
001 red lion 2 born with strength 6,1 lion in red headquarter
It's loyalty is 9
001 blue dragon 2 born with strength 3,1 dragon in blue headquarter
It has a arrow,and it's morale is 3.67
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
It has a sword and a bomb
003 red headquarter stops making warriors
003 blue iceman 4 born with strength 5,1 iceman in blue headquarter
It has a bomb
004 blue headquarter stops making warriors

  1 #include <iostream>
  2 #include <iomanip>
  3 #include <string>
  4 using namespace std;
  5 
  6 class headquarters
  7 {
  8 public:
  9 
 10     headquarters(const int theLifeValue, const int theRedOrBlue, const int theWarriorValue[],
 11                  const string theWarriorNames[], const int order[], const string theHeadquarterNames[]);
 12 
 13     ~headquarters();
 14 
 15     //获取出生数,用于战士的id计算
 16     int getCount() { return  count;}
 17 
 18     // 获取生命值,用于在main程序中与各战士的最小生命值比较,以及与战士生命值的比较
 19     int getLifeValue() { return lifeValue; }
 20 
 21     // 获取将要出生的战士的生命值
 22     int getWarriorValue(int position) { return warriorValues[position]; }
 23 
 24     // 获取将要出生的战士的名字
 25     string getWarriorName(int position) { return warriorNames[position];}
 26 
 27     // 生产战士
 28     void product(int time, int position);
 29 
 30 private:
 31     int lifeValue;
 32     int redOrBlue; // 红色总部值为0,蓝色总部值为1
 33     int count; // 生产的战士数量
 34     int *warriorCounts; // 记录每种战士数量的数组
 35     string headquarterName; // 总部的名字
 36     string *warriorNames; // 记录每种战士名字的数组
 37     int *warriorValues; // 记录每种战士生命值的数组
 38 
 39 };
 40 
 41 /**
 42  * 指定初始化
 43  */
 44 headquarters::headquarters(const int theLifeValue, const int theRedOrBlue, const int theWarriorValue[],
 45                            const string theWarriorNames[], const int order[], const string theHeadquarterNames[])
 46 {
 47     count = 0;
 48     lifeValue = theLifeValue;
 49     redOrBlue = theRedOrBlue;
 50     headquarterName = theHeadquarterNames[redOrBlue]; // 从总部名字的数组取得该总部的名字
 51     warriorCounts = new int[5]{0};
 52     warriorValues = new int[5];
 53     warriorNames = new string[5];
 54     for (int i = 0; i < 5; ++i) {
 55         warriorNames[i] = theWarriorNames[order[i]]; // 由给定的顺序和原始战士名字的数组,得到该总部战士名字的数组
 56         warriorValues[i] = theWarriorValue[order[i]]; // 由给定的顺序和原始战士名字的数组,得到该总部战士生命值的数组
 57     }
 58 }
 59 
 60 headquarters::~headquarters() {
 61     if (warriorCounts) delete []warriorCounts;
 62     if (warriorValues) delete []warriorValues;
 63     if (warriorNames) delete []warriorNames;
 64 }
 65 
 66 /**
 67      * 生产战士
 68      * time参数给定战士出生的回合
 69      * position参数给定该战士在司令部出生战士中的位置
 70      */
 71 void headquarters::product(int time, int position)
 72 {
 73     count++;
 74     warriorCounts[position]++; // 该种战士的总数加一
 75     // 输出题目要求的语句
 76     cout << setfill('0')<<setw(3) << time << " " << headquarterName << " " << warriorNames[position]
 77     << " " << count << " born with strength " << warriorValues[position] << "," << warriorCounts[position]
 78     << " " << warriorNames[position] << " in " << headquarterName << " headquarter" << endl;
 79     lifeValue -= warriorValues[position];
 80 }
 81 
 82 class warrior{
 83 private:
 84     int id;
 85     int lifeValue;
 86     string *weaponNames;
 87 public:
 88     warrior():id(0),lifeValue(0) {}
 89     warrior(int theId, int theLifeValue):id(theId), lifeValue(theLifeValue) {
 90         weaponNames = new string[3] {"sword", "bomb", "arrow"};
 91     }
 92     ~warrior() {
 93         if (weaponNames) delete []weaponNames;
 94     }
 95     int getId() { return id;}
 96     string* getWeaponNames() { return weaponNames;}
 97 };
 98 
 99 class dragon:public warrior {
100 private:
101     float morale; //士气属性
102 public:
103     dragon(int theId, int theLifeValue, int headquarterLifeValue):warrior(theId, theLifeValue) {
104         morale = (float)(headquarterLifeValue) / theLifeValue;
105     }
106     ~dragon() {};
107     float getMorale() { return morale;}
108     string getWeaponName() {
109         int index = this->getId() % 3;
110         return this->getWeaponNames()[index];
111     }
112     void print() {
113         cout<<"It has a "<<this->getWeaponName()<<",and it's morale is "<< setprecision(2)<<fixed<< this->getMorale()<<endl;
114     }
115 };
116 
117 class ninja:public warrior {
118 public:
119     ninja(int theId, int theLifeValue):warrior(theId, theLifeValue) {}
120     ~ninja() {}
121     string getFirstWeaponName() {
122         int index = this->getId() % 3;
123         return this->getWeaponNames()[index];
124     }
125     string getSecondWeaponName() {
126         int index = (this->getId() + 1) % 3;
127         return this->getWeaponNames()[index];
128     }
129     void print() {
130         cout<<"It has a "<<this->getFirstWeaponName()<<" and a "<<this->getSecondWeaponName()<<endl;
131     }
132 };
133 
134 class iceman:public warrior {
135 public:
136     iceman(int theId, int theLifeValue):warrior(theId, theLifeValue) {}
137     ~iceman() {};
138     string getWeaponName() {
139         int index = this->getId() % 3;
140         return this->getWeaponNames()[index];
141     }
142     void print() {
143         cout<<"It has a "<<this->getWeaponName()<<endl;
144     }
145 };
146 
147 class lion:public warrior {
148 private:
149     int loyalty;
150 public:
151     lion(int theId, int theLifeValue, int headquarterLifeValue):warrior(theId, theLifeValue) {
152         loyalty = headquarterLifeValue;}
153     ~lion() {}
154     int getLoyalty() { return loyalty;}
155     void print() {
156         cout<<"It's loyalty is "<<this->getLoyalty()<<endl;
157     }
158 };
159 
160 class wolf:public warrior {
161 public:
162     wolf(int theId, int theLifeValue):warrior(theId, theLifeValue) {}
163     ~wolf() {}
164 };
165 
166 int main()
167 {
168     const int redOrder[5] = {2, 3, 4, 1, 0}; // 红色总部的出兵顺序
169     const int blueOrder[5] = {3, 0, 1, 2, 4}; // 蓝色总部的出兵顺序
170     const string headquartersNames[2] = {"red", "blue"}; // 记录总部名字的数组
171     const string priorNames[5] = { "dragon", "ninja", "iceman", "lion", "wolf" }; // 记录战士名字的数组
172     int n = 0; // 测试数
173     cin >> n;
174     for (int i = 1; i <= n; i++)
175     {
176 
177         int priorValue[5], headquartersValue, minValue, redPosition = 0, bluePosition = 0;
178         bool redHadStop = false, blueHadStop = false;
179 
180         cin >> headquartersValue; // 获取总部生命值
181         // 获取每种战士的生命值
182         for (int j = 0; j < 5; j++)
183         {
184             cin >> priorValue[j];
185         }
186 
187         cout << "Case:" << i << endl;
188 
189         // 计算出战士中的最小生命值
190         minValue = priorValue[0];
191         for (int j = 1; j < 5; j++)
192         {
193             if (priorValue[j] < minValue)
194             {
195                 minValue = priorValue[j];
196             }
197         }
198 
199         // 初始化红军总部和蓝军总部
200         headquarters redOne = headquarters(headquartersValue, 0, priorValue, priorNames, redOrder, headquartersNames);
201         headquarters blueOne = headquarters(headquartersValue, 1, priorValue, priorNames, blueOrder, headquartersNames);
202 
203 
204         for (int time = 0;!redHadStop || !blueHadStop; time++)
205         {
206             // 如果红军没有停止出兵,继续
207             if (!redHadStop)
208             {
209                 // 红军的生命值小于最小战士生命值, 停止出兵,打印命令
210                 if (redOne.getLifeValue() < minValue)
211                 {
212                     cout << setfill('0')<<setw(3) << time << " red headquarter stops making warriors" << endl;
213                     redHadStop = true;
214                 }
215                 else {
216                     // 从上面的判断句筛选后,现在一定能出兵。
217                     // 从当前position开始增加,到某个位置出兵了停止
218                     while (true)
219                     {
220                         if (redOne.getLifeValue() >= redOne.getWarriorValue(redPosition))
221                         {
222                             redOne.product(time, redPosition); // 出兵
223 
224                             if (redOne.getWarriorName(redPosition) == "dragon") {
225                                 dragon theDragon(redOne.getCount(), redOne.getWarriorValue(redPosition), redOne.getLifeValue());
226                                 theDragon.print();
227                             } else if (redOne.getWarriorName(redPosition) == "ninja") {
228                                 ninja theNinja(redOne.getCount(), redOne.getWarriorValue(redPosition));
229                                 theNinja.print();
230                             } else if (redOne.getWarriorName(redPosition) == "iceman") {
231                                 iceman theIceman(redOne.getCount(), redOne.getWarriorValue(redPosition));
232                                 theIceman.print();
233                             } else if (redOne.getWarriorName(redPosition) == "lion") {
234                                 lion theLion(redOne.getCount(), redOne.getWarriorValue(redPosition), redOne.getLifeValue());
235                                 theLion.print();
236                             } else {
237                                 wolf theWolf(redOne.getCount(), redOne.getWarriorValue(redPosition));
238                             }
239 
240                             if (redPosition == 4 ? redPosition = 0: redPosition++);
241                             break;
242                         }
243                         else
244                         {
245                             if (redPosition == 4 ? redPosition = 0: redPosition++);
246                         }
247                     }
248                 }
249             }
250 
251             if (!blueHadStop)
252             {
253                 if (blueOne.getLifeValue() < minValue)
254                 {
255                     cout << setfill('0')<<setw(3)<< time << " blue headquarter stops making warriors" << endl;
256                     blueHadStop = true;
257                 }
258                 else {
259                     while (true)
260                     {
261                         if (blueOne.getLifeValue() >= blueOne.getWarriorValue(bluePosition))
262                         {
263                             blueOne.product(time, bluePosition);
264 
265                             if (blueOne.getWarriorName(bluePosition) == "dragon") {
266                                 dragon theDragon(blueOne.getCount(), blueOne.getWarriorValue(bluePosition), blueOne.getLifeValue());
267                                 theDragon.print();
268                             } else if (blueOne.getWarriorName(bluePosition) == "ninja") {
269                                 ninja theNinja(blueOne.getCount(), blueOne.getWarriorValue(bluePosition));
270                                 theNinja.print();
271                             } else if (blueOne.getWarriorName(bluePosition) == "iceman") {
272                                 iceman theIceman(blueOne.getCount(), blueOne.getWarriorValue(bluePosition));
273                                 theIceman.print();
274                             } else if (blueOne.getWarriorName(bluePosition) == "lion") {
275                                 lion theLion(blueOne.getCount(), blueOne.getWarriorValue(bluePosition), blueOne.getLifeValue());
276                                 theLion.print();
277                             } else {
278                                 wolf theWolf(blueOne.getCount(), blueOne.getWarriorValue(bluePosition));
279                             }
280 
281                             if (bluePosition == 4 ? bluePosition = 0: bluePosition++);
282                             break;
283                         }
284                         else
285                         {
286                             if (bluePosition == 4 ? bluePosition = 0: bluePosition++);
287                         }
288                     }
289                 }
290             }
291 
292         }
293 
294     }
295 
296     return 0;
297 }
原文地址:https://www.cnblogs.com/dagon/p/4762759.html