P大魔兽大作业(一)

P大的程序设计大作业,要求尽量运用类和对象来写。

......

小菜鸡在做之前没有领略到这一点,只是用类与对象定义了几个变量,关键部分还是基本的C++内容。

没接触过类和对象的朋友们可以参考一下。不过这样写的结果就是代码量出现大幅度增长,魔兽一就达到了将近一千行,以后还要做进一步精简。

运用类和对象会更好一点。

代码如下:

  1 #include<iostream>
  2 #include<cstring>
  3 #include<iomanip>
  4 #include<cmath>
  5 #include<cstdio>
  6 #include<cstdlib>
  7 using namespace std;
  8 class Redhead
  9 {
 10 public:
 11     int RMlife;//定义生命元
 12     int Riceman;
 13     int Rlion;
 14     int Rwolf;
 15     int Rninja;
 16     int Rdragon;//定义红魔军司令部各个武士的数量
 17     char Rs[5][20] = { "iceman","lion","wolf","ninja","dragon" };
 18     Redhead(int i) :RMlife(i)
 19     {
 20         Riceman = 0;
 21         Rlion = 0;
 22         Rwolf = 0;
 23         Rninja = 0;
 24         Rdragon = 0;
 25     }
 26     Redhead()
 27     {
 28 
 29     }
 30     void Rtozero()
 31     {
 32         Riceman = 0;
 33         Rlion = 0;
 34         Rwolf = 0;
 35         Rninja = 0;
 36         Rdragon = 0;
 37         RMlife = 0;
 38     }
 39 };
 40 class Bluehead
 41 {
 42 public:
 43     int BMlife;//定义生命元
 44     int Biceman;
 45     int Blion;
 46     int Bwolf;
 47     int Bninja;
 48     int Bdragon;//定义红魔军司令部各个武士的数量
 49     char Bs[5][20] = { "lion","dragon","ninja","iceman","wolf" };
 50     Bluehead(int i) :BMlife(i)
 51     {
 52         Biceman = 0;
 53         Blion = 0;
 54         Bwolf = 0;
 55         Bninja = 0;
 56         Bdragon = 0;
 57     }
 58     Bluehead()
 59     {
 60 
 61     }
 62     void Btozero()
 63     {
 64         Biceman = 0;
 65         Blion = 0;
 66         Bwolf = 0;
 67         Bninja = 0;
 68         Bdragon = 0;
 69         BMlife = 0;
 70     }
 71 };
 72 void Red1();
 73 void Red2();
 74 void Red3();
 75 void Red4();
 76 void Red5();
 77 void Blue1();
 78 void Blue2();
 79 void Blue3();
 80 void Blue4();
 81 void Blue5();
 82 int M;
 83 Redhead Cred;
 84 Bluehead Cblue;
 85 int Miceman;
 86 int Mlion;
 87 int Mwolf;
 88 int Mninja;
 89 int Mdragon;
 90 int Flag = 0;
 91 int BFlag = 0;
 92 int rflag = 1;
 93 int bflag = 1;
 94 int i;
 95 int main()//--------------------------------------------------------------------------------------//
 96 {
 97     int RRR = 0;
 98     int BBB = 0;
 99     int RR = 0;
100     int BB = 0;
101     int Rv = 0;
102     int Bv = 0;
103     int Ntest;
104     cin >> Ntest;
105     for (int NTEST = 1; NTEST <= Ntest; NTEST++)
106     {
107         cout << "Case:" << NTEST << endl;
108         int M;
109         cin >> M;
110         cin >> Mdragon >> Mninja >> Miceman >> Mlion >> Mwolf;
111         Cred = M;
112         Cblue = M;
113         for (i = 0;; i++)
114         {
115             if (rflag == 1)
116             {
117                 Red1();
118             }
119             else
120             {
121                 if (rflag == 2)
122                 {
123                     Red2();
124                 }
125                 else
126                 {
127                     if (rflag == 3)
128                     {
129                         Red3();
130                     }
131                     else
132                     {
133                         if (rflag == 4)
134                         {
135                             Red4();
136                         }
137                         else
138                         {
139                             if (rflag == 5)
140                             {
141                                 Red5();
142                             }
143                         }
144                     }
145                 }
146             }
147             if (Flag == 0&&Rv==0)
148             {
149                 Rv = 1;
150                 cout << setfill('0') << setw(3) << i << " red headquarter stops making warriors" << endl;
151                 RRR = 1;
152             }
153             else
154             {
155                 Flag = 0;
156             }
157             //-----//
158             if (bflag == 1)
159             {
160                 Blue1();
161             }
162             else
163             {
164                 if (bflag == 2)
165                 {
166                     Blue2();
167                 }
168                 else
169                 {
170                     if (bflag == 3)
171                     {
172                         Blue3();
173                     }
174                     else
175                     {
176                         if (bflag == 4)
177                         {
178                             Blue4();
179                         }
180                         else
181                         {
182                             if (bflag == 5)
183                             {
184                                 Blue5();
185                             }
186                         }
187                     }
188                 }
189             }
190             if (BFlag == 0&&Bv==0)
191             {
192                 Bv = 1;
193                 cout << setfill('0') << setw(3) << i << " blue headquarter stops making warriors" << endl;
194                 BBB = 1;
195                 
196             }
197             else
198             {
199                 BFlag = 0;
200             }
201             if (Bv == 1 && Rv == 1)
202             {
203                 break;
204             }
205         }
206         Bv = 0;
207         Rv = 0;
208         Cred.Rtozero();
209         Cblue.Btozero();
210         Flag = 0;
211         BFlag = 0;
212         rflag = 1;
213         bflag = 1;
214         RRR = 0;
215         BBB = 0;
216         RR = 0;
217         BB = 0;
218     }
219     return 0;
220 }
221 //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
222 void Red1()
223 {
224     if (Cred.RMlife - Miceman >= 0)
225     {
226         Cred.RMlife -= Miceman;
227         Cred.Riceman++;
228         cout << setfill('0') << setw(3) << i;
229         cout << " ";
230         cout << "red " << "iceman" << " " << i + 1 << " born with strength " << Miceman << "," << Cred.Riceman << " iceman in red headquarter" << endl;
231         Flag = 1;
232         rflag = 2;
233     }
234     else
235     {
236         if (Cred.RMlife - Mlion >= 0)
237         {
238             Cred.RMlife -= Mlion;
239             Cred.Rlion++;
240             cout << setfill('0') << setw(3) << i;
241             cout << " ";
242             cout << "red " << "lion" << " " << i + 1 << " born with strength " << Mlion << "," << Cred.Rlion << " lion in red headquarter" << endl;
243             Flag = 1;
244             rflag = 3;
245         }
246         else
247         {
248             if (Cred.RMlife - Mwolf >= 0)
249             {
250                 Cred.RMlife -= Mwolf;
251                 Cred.Rwolf++;
252                 cout << setfill('0') << setw(3) << i;
253                 cout << " ";
254                 cout << "red " << "wolf" << " " << i + 1 << " born with strength " << Mwolf << "," << Cred.Rwolf << " wolf in red headquarter" << endl;
255                 Flag = 1;
256                 rflag = 4;
257             }
258             else
259             {
260                 if (Cred.RMlife - Mninja >= 0)
261                 {
262                     Cred.RMlife -= Mninja;
263                     Cred.Rninja++;
264                     cout << setfill('0') << setw(3) << i;
265                     cout << " ";
266                     cout << "red " << "ninja" << " " << i + 1 << " born with strength " << Mninja << "," << Cred.Rninja << " ninja in red headquarter" << endl;
267                     Flag = 1;
268                     rflag = 5;
269                 }
270                 else
271                 {
272                     if (Cred.RMlife - Mdragon >= 0)
273                     {
274                         Cred.RMlife -= Mdragon;
275                         Cred.Rdragon++;
276                         cout << setfill('0') << setw(3) << i;
277                         cout << " ";
278                         cout << "red " << "dragon" << " " << i + 1 << " born with strength " << Mdragon << "," << Cred.Rdragon << " dragon in red headquarter" << endl;
279                         Flag = 1;
280                         rflag = 1;
281                     }
282                 }
283             }
284         }
285     }
286 }
287 void Red2()
288 {
289     if (Cred.RMlife - Mlion >= 0)
290     {
291         Cred.RMlife -= Mlion;
292         Cred.Rlion++;
293         cout << setfill('0') << setw(3) << i;
294         cout << " ";
295         cout << "red " << "lion" << " " << i + 1 << " born with strength " << Mlion << "," << Cred.Rlion << " lion in red headquarter" << endl;
296         Flag = 1;
297         rflag = 3;
298     }
299     else
300     {
301         if (Cred.RMlife - Mwolf >= 0)
302         {
303             Cred.RMlife -= Mwolf;
304             Cred.Rwolf++;
305             cout << setfill('0') << setw(3) << i;
306             cout << " ";
307             cout << "red " << "wolf" << " " << i + 1 << " born with strength " << Mwolf << "," << Cred.Rwolf << " wolf in red headquarter" << endl;
308             Flag = 1;
309             rflag = 4;
310         }
311         else
312         {
313             if (Cred.RMlife - Mninja >= 0)
314             {
315                 Cred.RMlife -= Mninja;
316                 Cred.Rninja++;
317                 cout << setfill('0') << setw(3) << i;
318                 cout << " ";
319                 cout << "red " << "ninja" << " " << i + 1 << " born with strength " << Mninja << "," << Cred.Rninja << " ninja in red headquarter" << endl;
320                 Flag = 1;
321                 rflag = 5;
322             }
323             else
324             {
325                 if (Cred.RMlife - Mdragon >= 0)
326                 {
327                     Cred.RMlife -= Mdragon;
328                     Cred.Rdragon++;
329                     cout << setfill('0') << setw(3) << i;
330                     cout << " ";
331                     cout << "red " << "dragon" << " " << i + 1 << " born with strength " << Mdragon << "," << Cred.Rdragon << " dragon in red headquarter" << endl;
332                     Flag = 1;
333                     rflag = 1;
334                 }
335                 else
336                 {
337                     if (Cred.RMlife - Miceman >= 0)
338                     {
339                         Cred.RMlife -= Miceman;
340                         Cred.Riceman++;
341                         cout << setfill('0') << setw(3) << i;
342                         cout << " ";
343                         cout << "red " << "iceman" << " " << i + 1 << " born with strength " << Miceman << "," << Cred.Riceman << " iceman in red headquarter" << endl;
344                         Flag = 1;
345                         rflag = 2;
346                     }
347                 }
348             }
349         }
350     }
351 
352 }
353 void Red3()
354 {
355     if (Cred.RMlife - Mwolf >= 0)
356     {
357         Cred.RMlife -= Mwolf;
358         Cred.Rwolf++;
359         cout << setfill('0') << setw(3) << i;
360         cout << " ";
361         cout << "red " << "wolf" << " " << i + 1 << " born with strength " << Mwolf << "," << Cred.Rwolf << " wolf in red headquarter" << endl;
362         Flag = 1;
363         rflag = 4;
364     }
365     else
366     {
367         if (Cred.RMlife - Mninja >= 0)
368         {
369             Cred.RMlife -= Mninja;
370             Cred.Rninja++;
371             cout << setfill('0') << setw(3) << i;
372             cout << " ";
373             cout << "red " << "ninja" << " " << i + 1 << " born with strength " << Mninja << "," << Cred.Rninja << " ninja in red headquarter" << endl;
374             Flag = 1;
375             rflag = 5;
376         }
377         else
378         {
379             if (Cred.RMlife - Mdragon >= 0)
380             {
381                 Cred.RMlife -= Mdragon;
382                 Cred.Rdragon++;
383                 cout << setfill('0') << setw(3) << i;
384                 cout << " ";
385                 cout << "red " << "dragon" << " " << i + 1 << " born with strength " << Mdragon << "," << Cred.Rdragon << " dragon in red headquarter" << endl;
386                 Flag = 1;
387                 rflag = 1;
388             }
389             else
390             {
391                 if (Cred.RMlife - Miceman >= 0)
392                 {
393                     Cred.RMlife -= Miceman;
394                     Cred.Riceman++;
395                     cout << setfill('0') << setw(3) << i;
396                     cout << " ";
397                     cout << "red " << "iceman" << " " << i + 1 << " born with strength " << Miceman << "," << Cred.Riceman << " iceman in red headquarter" << endl;
398                     Flag = 1;
399                     rflag = 2;
400                 }
401                 else
402                 {
403                     if (Cred.RMlife - Mlion >= 0)
404                     {
405                         Cred.RMlife -= Mlion;
406                         Cred.Rlion++;
407                         cout << setfill('0') << setw(3) << i;
408                         cout << " ";
409                         cout << "red " << "lion" << " " << i + 1 << " born with strength " << Mlion << "," << Cred.Rlion << " lion in red headquarter" << endl;
410                         Flag = 1;
411                         rflag = 3;
412                     }
413                 }
414             }
415         }
416     }
417 }
418 void Red4()
419 {
420     if (Cred.RMlife - Mninja >= 0)
421     {
422         Cred.RMlife -= Mninja;
423         Cred.Rninja++;
424         cout << setfill('0') << setw(3) << i;
425         cout << " ";
426         cout << "red " << "ninja" << " " << i + 1 << " born with strength " << Mninja << "," << Cred.Rninja << " ninja in red headquarter" << endl;
427         Flag = 1;
428         rflag = 5;
429     }
430     else
431     {
432         if (Cred.RMlife - Mdragon >= 0)
433         {
434             Cred.RMlife -= Mdragon;
435             Cred.Rdragon++;
436             cout << setfill('0') << setw(3) << i;
437             cout << " ";
438             cout << "red " << "dragon" << " " << i + 1 << " born with strength " << Mdragon << "," << Cred.Rdragon << " dragon in red headquarter" << endl;
439             Flag = 1;
440             rflag = 1;
441         }
442         else
443         {
444             if (Cred.RMlife - Miceman >= 0)
445             {
446                 Cred.RMlife -= Miceman;
447                 Cred.Riceman++;
448                 cout << setfill('0') << setw(3) << i;
449                 cout << " ";
450                 cout << "red " << "iceman" << " " << i + 1 << " born with strength " << Miceman << "," << Cred.Riceman << " iceman in red headquarter" << endl;
451                 Flag = 1;
452                 rflag = 2;
453             }
454             else
455             {
456                 if (Cred.RMlife - Mlion >= 0)
457                 {
458                     Cred.RMlife -= Mlion;
459                     Cred.Rlion++;
460                     cout << setfill('0') << setw(3) << i;
461                     cout << " ";
462                     cout << "red " << "lion" << " " << i + 1 << " born with strength " << Mlion << "," << Cred.Rlion << " lion in red headquarter" << endl;
463                     Flag = 1;
464                     rflag = 3;
465                 }
466                 else
467                 {
468                     if (Cred.RMlife - Mwolf >= 0)
469                     {
470                         Cred.RMlife -= Mwolf;
471                         Cred.Rwolf++;
472                         cout << setfill('0') << setw(3) << i;
473                         cout << " ";
474                         cout << "red " << "wolf" << " " << i + 1 << " born with strength " << Mwolf << "," << Cred.Rwolf << " wolf in red headquarter" << endl;
475                         Flag = 1;
476                         rflag = 4;
477                     }
478                 }
479             }
480         }
481     }
482 }
483 void Red5()
484 {
485     if (Cred.RMlife - Mdragon >= 0)
486     {
487         Cred.RMlife -= Mdragon;
488         Cred.Rdragon++;
489         cout << setfill('0') << setw(3) << i;
490         cout << " ";
491         cout << "red " << "dragon" << " " << i + 1 << " born with strength " << Mdragon << "," << Cred.Rdragon << " dragon in red headquarter" << endl;
492         Flag = 1;
493         rflag = 1;
494     }
495     else
496     {
497         if (Cred.RMlife - Miceman >= 0)
498         {
499             Cred.RMlife -= Miceman;
500             Cred.Riceman++;
501             cout << setfill('0') << setw(3) << i;
502             cout << " ";
503             cout << "red " << "iceman" << " " << i + 1 << " born with strength " << Miceman << "," << Cred.Riceman << " iceman in red headquarter" << endl;
504             Flag = 1;
505             rflag = 2;
506         }
507         else
508         {
509             if (Cred.RMlife - Mlion >= 0)
510             {
511                 Cred.RMlife -= Mlion;
512                 Cred.Rlion++;
513                 cout << setfill('0') << setw(3) << i;
514                 cout << " ";
515                 cout << "red " << "lion" << " " << i + 1 << " born with strength " << Mlion << "," << Cred.Rlion << " lion in red headquarter" << endl;
516                 Flag = 1;
517                 rflag = 3;
518             }
519             else
520             {
521                 if (Cred.RMlife - Mwolf >= 0)
522                 {
523                     Cred.RMlife -= Mwolf;
524                     Cred.Rwolf++;
525                     cout << setfill('0') << setw(3) << i;
526                     cout << " ";
527                     cout << "red " << "wolf" << " " << i + 1 << " born with strength " << Mwolf << "," << Cred.Rwolf << " wolf in red headquarter" << endl;
528                     Flag = 1;
529                     rflag = 4;
530                 }
531                 else
532                 {
533                     if (Cred.RMlife - Mninja >= 0)
534                     {
535                         Cred.RMlife -= Mninja;
536                         Cred.Rninja++;
537                         cout << setfill('0') << setw(3) << i;
538                         cout << " ";
539                         cout << "red " << "ninja" << " " << i + 1 << " born with strength " << Mninja << "," << Cred.Rninja << " ninja in red headquarter" << endl;
540                         Flag = 1;
541                         rflag = 5;
542                     }
543                 }
544             }
545         }
546     }
547 }//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
548 //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------blue//
549 void Blue1()
550 {
551     if (Cblue.BMlife - Mlion >= 0)
552     {
553         Cblue.BMlife -= Mlion;
554         Cblue.Blion++;
555         cout << setfill('0') << setw(3) << i;
556         cout << " ";
557         cout << "blue " << "lion" << " " << i + 1 << " born with strength " << Mlion << "," << Cblue.Blion << " lion in blue headquarter" << endl;
558         BFlag = 1;
559         bflag = 2;
560     }
561     else
562     {
563         if (Cblue.BMlife - Mdragon >= 0)
564         {
565             Cblue.BMlife -= Mdragon;
566             Cblue.Bdragon++;
567             cout << setfill('0') << setw(3) << i;
568             cout << " ";
569             cout << "blue " << "dragon" << " " << i + 1 << " born with strength " << Mdragon << "," << Cblue.Bdragon << " dragon in blue headquarter" << endl;
570             BFlag = 1;
571             bflag = 3;
572         }
573         else
574         {
575             if (Cblue.BMlife - Mninja >= 0)
576             {
577                 Cblue.BMlife -= Mninja;
578                 Cblue.Bninja++;
579                 cout << setfill('0') << setw(3) << i;
580                 cout << " ";
581                 cout << "blue " << "ninja" << " " << i + 1 << " born with strength " << Mninja << "," << Cblue.Bninja << " ninja in blue headquarter" << endl;
582                 BFlag = 1;
583                 bflag = 4;
584             }
585             else
586             {
587                 if (Cblue.BMlife - Miceman >= 0)
588                 {
589                     Cblue.BMlife -= Miceman;
590                     Cblue.Biceman++;
591                     cout << setfill('0') << setw(3) << i;
592                     cout << " ";
593                     cout << "blue " << "iceman" << " " << i + 1 << " born with strength " << Miceman << "," << Cblue.Biceman << " iceman in blue headquarter" << endl;
594                     BFlag = 1;
595                     bflag = 5;
596                 }
597                 else
598                 {
599                     if (Cblue.BMlife - Mwolf >= 0)
600                     {
601                         Cblue.BMlife -= Mwolf;
602                         Cblue.Bwolf++;
603                         cout << setfill('0') << setw(3) << i;
604                         cout << " ";
605                         cout << "blue " << "wolf" << " " << i + 1 << " born with strength " << Mwolf << "," << Cblue.Bwolf << " wolf in blue headquarter" << endl;
606                         BFlag = 1;
607                         bflag = 1;
608                     }
609                 }
610             }
611         }
612     }
613 }
614 void Blue2()
615 {
616     if (Cblue.BMlife - Mdragon >= 0)
617     {
618         Cblue.BMlife -= Mdragon;
619         Cblue.Bdragon++;
620         cout << setfill('0') << setw(3) << i;
621         cout << " ";
622         cout << "blue " << "dragon" << " " << i + 1 << " born with strength " << Mdragon << "," << Cblue.Bdragon << " dragon in blue headquarter" << endl;
623         BFlag = 1;
624         bflag = 3;
625     }
626     else
627     {
628         if (Cblue.BMlife - Mninja >= 0)
629         {
630             Cblue.BMlife -= Mninja;
631             Cblue.Bninja++;
632             cout << setfill('0') << setw(3) << i;
633             cout << " ";
634             cout << "blue " << "ninja" << " " << i + 1 << " born with strength " << Mninja << "," << Cblue.Bninja << " ninja in blue headquarter" << endl;
635             BFlag = 1;
636             bflag = 4;
637         }
638         else
639         {
640             if (Cblue.BMlife - Miceman >= 0)
641             {
642                 Cblue.BMlife -= Miceman;
643                 Cblue.Biceman++;
644                 cout << setfill('0') << setw(3) << i;
645                 cout << " ";
646                 cout << "blue " << "iceman" << " " << i + 1 << " born with strength " << Miceman << "," << Cblue.Biceman << " iceman in blue headquarter" << endl;
647                 BFlag = 1;
648                 bflag = 5;
649             }
650             else
651             {
652                 if (Cblue.BMlife - Mwolf >= 0)
653                 {
654                     Cblue.BMlife -= Mwolf;
655                     Cblue.Bwolf++;
656                     cout << setfill('0') << setw(3) << i;
657                     cout << " ";
658                     cout << "blue " << "wolf" << " " << i + 1 << " born with strength " << Mwolf << "," << Cblue.Bwolf << " wolf in blue headquarter" << endl;
659                     BFlag = 1;
660                     bflag = 1;
661                 }
662                 else
663                 {
664                     if (Cblue.BMlife - Mlion >= 0)
665                     {
666                         Cblue.BMlife -= Mlion;
667                         Cblue.Blion++;
668                         cout << setfill('0') << setw(3) << i;
669                         cout << " ";
670                         cout << "blue " << "lion" << " " << i + 1 << " born with strength " << Mlion << "," << Cblue.Blion << " lion in blue headquarter" << endl;
671                         BFlag = 1;
672                         bflag = 2;
673                     }
674                 }
675             }
676         }
677     }
678 }
679 void Blue3()
680 {
681     if (Cblue.BMlife - Mninja >= 0)
682     {
683         Cblue.BMlife -= Mninja;
684         Cblue.Bninja++;
685         cout << setfill('0') << setw(3) << i;
686         cout << " ";
687         cout << "blue " << "ninja" << " " << i + 1 << " born with strength " << Mninja << "," << Cblue.Bninja << " ninja in blue headquarter" << endl;
688         BFlag = 1;
689         bflag = 4;
690     }
691     else
692     {
693         if (Cblue.BMlife - Miceman >= 0)
694         {
695             Cblue.BMlife -= Miceman;
696             Cblue.Biceman++;
697             cout << setfill('0') << setw(3) << i;
698             cout << " ";
699             cout << "blue " << "iceman" << " " << i + 1 << " born with strength " << Miceman << "," << Cblue.Biceman << " iceman in blue headquarter" << endl;
700             BFlag = 1;
701             bflag = 5;
702         }
703         else
704         {
705             if (Cblue.BMlife - Mwolf >= 0)
706             {
707                 Cblue.BMlife -= Mwolf;
708                 Cblue.Bwolf++;
709                 cout << setfill('0') << setw(3) << i;
710                 cout << " ";
711                 cout << "blue " << "wolf" << " " << i + 1 << " born with strength " << Mwolf << "," << Cblue.Bwolf << " wolf in blue headquarter" << endl;
712                 BFlag = 1;
713                 bflag = 1;
714             }
715             else
716             {
717                 if (Cblue.BMlife - Mlion >= 0)
718                 {
719                     Cblue.BMlife -= Mlion;
720                     Cblue.Blion++;
721                     cout << setfill('0') << setw(3) << i;
722                     cout << " ";
723                     cout << "blue " << "lion" << " " << i + 1 << " born with strength " << Mlion << "," << Cblue.Blion << " lion in blue headquarter" << endl;
724                     BFlag = 1;
725                     bflag = 2;
726                 }
727                 else
728                 {
729                     if (Cblue.BMlife - Mdragon >= 0)
730                     {
731                         Cblue.BMlife -= Mdragon;
732                         Cblue.Bdragon++;
733                         cout << setfill('0') << setw(3) << i;
734                         cout << " ";
735                         cout << "blue " << "dragon" << " " << i + 1 << " born with strength " << Mdragon << "," << Cblue.Bdragon << " dragon in blue headquarter" << endl;
736                         BFlag = 1;
737                         bflag = 3;
738                     }
739                 }
740             }
741         }
742     }
743 }
744 void Blue4()
745 {
746     if (Cblue.BMlife - Miceman >= 0)
747     {
748         Cblue.BMlife -= Miceman;
749         Cblue.Biceman++;
750         cout << setfill('0') << setw(3) << i;
751         cout << " ";
752         cout << "blue " << "iceman" << " " << i + 1 << " born with strength " << Miceman << "," << Cblue.Biceman << " iceman in blue headquarter" << endl;
753         BFlag = 1;
754         bflag = 5;
755     }
756     else
757     {
758         if (Cblue.BMlife - Mwolf >= 0)
759         {
760             Cblue.BMlife -= Mwolf;
761             Cblue.Bwolf++;
762             cout << setfill('0') << setw(3) << i;
763             cout << " ";
764             cout << "blue " << "wolf" << " " << i + 1 << " born with strength " << Mwolf << "," << Cblue.Bwolf << " wolf in blue headquarter" << endl;
765             BFlag = 1;
766             bflag = 1;
767         }
768         else
769         {
770             if (Cblue.BMlife - Mlion >= 0)
771             {
772                 Cblue.BMlife -= Mlion;
773                 Cblue.Blion++;
774                 cout << setfill('0') << setw(3) << i;
775                 cout << " ";
776                 cout << "blue " << "lion" << " " << i + 1 << " born with strength " << Mlion << "," << Cblue.Blion << " lion in blue headquarter" << endl;
777                 BFlag = 1;
778                 bflag = 2;
779             }
780             else
781             {
782                 if (Cblue.BMlife - Mdragon >= 0)
783                 {
784                     Cblue.BMlife -= Mdragon;
785                     Cblue.Bdragon++;
786                     cout << setfill('0') << setw(3) << i;
787                     cout << " ";
788                     cout << "blue " << "dragon" << " " << i + 1 << " born with strength " << Mdragon << "," << Cblue.Bdragon << " dragon in blue headquarter" << endl;
789                     BFlag = 1;
790                     bflag = 3;
791                 }
792                 else
793                 {
794                     if (Cblue.BMlife - Mninja >= 0)
795                     {
796                         Cblue.BMlife -= Mninja;
797                         Cblue.Bninja++;
798                         cout << setfill('0') << setw(3) << i;
799                         cout << " ";
800                         cout << "blue " << "ninja" << " " << i + 1 << " born with strength " << Mninja << "," << Cblue.Bninja << " ninja in blue headquarter" << endl;
801                         BFlag = 1;
802                         bflag = 4;
803                     }
804                 }
805             }
806         }
807     }
808 }
809 void Blue5()
810 {
811     if (Cblue.BMlife - Mwolf >= 0)
812     {
813         Cblue.BMlife -= Mwolf;
814         Cblue.Bwolf++;
815         cout << setfill('0') << setw(3) << i;
816         cout << " ";
817         cout << "blue " << "wolf" << " " << i + 1 << " born with strength " << Mwolf << "," << Cblue.Bwolf << " wolf in blue headquarter" << endl;
818         BFlag = 1;
819         bflag = 1;
820     }
821     else
822     {
823         if (Cblue.BMlife - Mlion >= 0)
824         {
825             Cblue.BMlife -= Mlion;
826             Cblue.Blion++;
827             cout << setfill('0') << setw(3) << i;
828             cout << " ";
829             cout << "blue " << "lion" << " " << i + 1 << " born with strength " << Mlion << "," << Cblue.Blion << " lion in blue headquarter" << endl;
830             BFlag = 1;
831             bflag = 2;
832         }
833         else
834         {
835             if (Cblue.BMlife - Mdragon >= 0)
836             {
837                 Cblue.BMlife -= Mdragon;
838                 Cblue.Bdragon++;
839                 cout << setfill('0') << setw(3) << i;
840                 cout << " ";
841                 cout << "blue " << "dragon" << " " << i + 1 << " born with strength " << Mdragon << "," << Cblue.Bdragon << " dragon in blue headquarter" << endl;
842                 BFlag = 1;
843                 bflag = 3;
844             }
845             else
846             {
847                 if (Cblue.BMlife - Mninja >= 0)
848                 {
849                     Cblue.BMlife -= Mninja;
850                     Cblue.Bninja++;
851                     cout << setfill('0') << setw(3) << i;
852                     cout << " ";
853                     cout << "blue " << "ninja" << " " << i + 1 << " born with strength " << Mninja << "," << Cblue.Bninja << " ninja in blue headquarter" << endl;
854                     BFlag = 1;
855                     bflag = 4;
856                 }
857                 else
858                 {
859                     if (Cblue.BMlife - Miceman >= 0)
860                     {
861                         Cblue.BMlife -= Miceman;
862                         Cblue.Biceman++;
863                         cout << setfill('0') << setw(3) << i;
864                         cout << " ";
865                         cout << "blue " << "iceman" << " " << i + 1 << " born with strength " << Miceman << "," << Cblue.Biceman << " iceman in blue headquarter" << endl;
866                         BFlag = 1;
867                         bflag = 5;
868                     }
869                 }
870             }
871         }
872     }
873 }

关键部分就在于最下面这几个函数,看似很多,也不过就是五个基本函数来回交换顺序罢了。

这道题唯一考验技巧的地方就是武士的输出顺序。应该如何做到按顺序且一个武士生命元不够后去制造下一个武士。

我用的是给函数以不同的标记,通过标记对函数进行多轮的循环。

posted @ 2019-03-25 10:37  乐颐  阅读(354)  评论(0)    收藏  举报