zoukankan      html  css  js  c++  java
  • POJ C++程序设计 编程题#4:魔兽世界之一:备战

    编程题#4:魔兽世界之一:备战

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

    注意: 总时间限制: 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分开始到双方司令部停止制造武士为止的所有事件按顺序输出。

    一共有两种事件,其对应的输出样例如下:

    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

      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     // 获取生命值,用于在main程序中与各战士的最小生命值比较,以及与战士生命值的比较
     16     int getLifeValue() { return lifeValue; };
     17 
     18     // 获取将要出生的战士的生命值
     19     int getWarriorValue(int position) { return warriorValues[position]; };
     20 
     21     void product(int time, int position);
     22     
     23 private:
     24     int lifeValue;
     25     int redOrBlue; // 红色总部值为0,蓝色总部值为1
     26     int count; // 生产的战士数量
     27     int warriorCounts[5]; // 记录每种战士数量的数组
     28     string headquarterName; // 总部的名字
     29     string warriorNames[5]; // 记录每种战士名字的数组
     30     int warriorValues[5]; // 记录每种战士生命值的数组
     31 
     32 };
     33 
     34 /**
     35  * 指定初始化
     36  */
     37 headquarters::headquarters(const int theLifeValue, const int theRedOrBlue, const int theWarriorValue[],
     38                            const string theWarriorNames[], const int order[], const string theHeadquarterNames[])
     39 {
     40     count = 0;
     41     lifeValue = theLifeValue;
     42     redOrBlue = theRedOrBlue;
     43     headquarterName = theHeadquarterNames[redOrBlue]; // 从总部名字的数组取得该总部的名字
     44     for (int i = 0; i < 5; ++i) {
     45         warriorCounts[i] = 0;
     46         warriorNames[i] = theWarriorNames[order[i]]; // 由给定的顺序和原始战士名字的数组,得到该总部战士名字的数组
     47         warriorValues[i] = theWarriorValue[order[i]]; // 由给定的顺序和原始战士名字的数组,得到该总部战士生命值的数组
     48     }
     49 }
     50 
     51 /**
     52      * 生产战士
     53      * time参数给定战士出生的回合
     54      * position参数给定该战士在司令部出生战士中的位置
     55      */
     56 void headquarters::product(int time, int position)
     57 {
     58     count++;
     59     warriorCounts[position]++; // 该种战士的总数加一
     60     // 输出题目要求的语句
     61     cout << setfill('0')<<setw(3) << time << " " << headquarterName << " " << warriorNames[position]
     62     << " " << count << " born with strength " << warriorValues[position] << "," << warriorCounts[position]
     63     << " " << warriorNames[position] << " in " << headquarterName << " headquarter" << endl;
     64     lifeValue -= warriorValues[position];
     65 }
     66 
     67 int main()
     68 {
     69     const int redOrder[5] = {2, 3, 4, 1, 0}; // 红色总部的出兵顺序
     70     const int blueOrder[5] = {3, 0, 1, 2, 4}; // 蓝色总部的出兵顺序
     71     const string headquartersNames[2] = {"red", "blue"}; // 记录总部名字的数组
     72     const string priorNames[5] = { "dragon", "ninja", "iceman", "lion", "wolf" }; // 记录战士名字的数组
     73     int n = 0; // 测试数
     74     cin >> n;
     75     for (int i = 1; i <= n; i++)
     76     {
     77 
     78         int priorValue[5], headquartersValue, minValue, redPosition = 0, bluePosition = 0;
     79         bool redHadStop = false, blueHadStop = false;
     80 
     81         cin >> headquartersValue; // 获取总部生命值
     82         // 获取每种战士的生命值
     83         for (int j = 0; j < 5; j++)
     84         {
     85             cin >> priorValue[j];
     86         }
     87         
     88         cout << "Case:" << i << endl;
     89 
     90         // 计算出战士中的最小生命值
     91         minValue = priorValue[0];
     92         for (int j = 1; j < 5; j++)
     93         {
     94             if (priorValue[j] < minValue)
     95             {
     96                 minValue = priorValue[j];
     97             }
     98         }
     99 
    100         // 初始化红军总部和蓝军总部
    101         headquarters redOne = headquarters(headquartersValue, 0, priorValue, priorNames, redOrder, headquartersNames);
    102         headquarters blueOne = headquarters(headquartersValue, 1, priorValue, priorNames, blueOrder, headquartersNames);
    103 
    104 
    105         for (int time = 0;!redHadStop || !blueHadStop; time++)
    106         {
    107             // 如果红军没有停止出兵,继续
    108             if (!redHadStop)
    109             {
    110                 // 红军的生命值小于最小战士生命值, 停止出兵,打印命令
    111                 if (redOne.getLifeValue() < minValue)
    112                 {
    113                     cout << setfill('0')<<setw(3) << time << " red headquarter stops making warriors" << endl;
    114                     redHadStop = true;
    115                 }
    116                 else {
    117                     // 从上面的判断句筛选后,现在一定能出兵。
    118                     // 从当前position开始增加,到某个位置出兵了停止
    119                     while (true)
    120                     {
    121                         if (redOne.getLifeValue() >= redOne.getWarriorValue(redPosition))
    122                         {
    123                             redOne.product(time, redPosition);
    124                             if (redPosition == 4 ? redPosition = 0: redPosition++);
    125                             break;
    126                         }
    127                         else
    128                         {
    129                             if (redPosition == 4 ? redPosition = 0: redPosition++);
    130                         }
    131                     }
    132                 }
    133             }
    134             
    135             if (!blueHadStop)
    136             {
    137                 if (blueOne.getLifeValue() < minValue)
    138                 {
    139                     cout << setfill('0')<<setw(3)<< time << " blue headquarter stops making warriors" << endl;
    140                     blueHadStop = true;
    141                 }
    142                 else {
    143                     while (true)
    144                     {
    145                         if (blueOne.getLifeValue() >= blueOne.getWarriorValue(bluePosition))
    146                         {
    147                             blueOne.product(time, bluePosition);
    148                             if (bluePosition == 4 ? bluePosition = 0: bluePosition++);
    149                             break;
    150                         }
    151                         else
    152                         {
    153                             if (bluePosition == 4 ? bluePosition = 0: bluePosition++);
    154                         }
    155                     }
    156                 }
    157             }
    158             
    159         }
    160         
    161     }
    162     
    163     return 0;
    164 }
  • 相关阅读:
    50个网页常用小代码
    web前端题目(持续更新)
    一步步构建大型网站架构(转)
    CentOS下配置node.js
    ajax文件上传
    test
    文件上传input简便美化方案
    String.match()与RegExp.exec()
    ie7下zindex问题
    javascript将数组插入到另一个数组中
  • 原文地址:https://www.cnblogs.com/dagon/p/4750100.html
Copyright © 2011-2022 走看看