类和对象 编程题#4:魔兽世界之一:备战(Coursera 程序设计与算法 专项课程3 C++程序设计 郭炜、刘家瑛;OpenJudge)

来源:互联网 发布:小米平板2windows版 编辑:程序博客网 时间:2024/06/05 05:55

编程题#4:魔兽世界之一:备战
大体框架是参考网上的,但是自己在尝试时进行了不少细节上的改动和注释,分享给大家,共勉!

来源: POJ (http://cxsjsxmooc.openjudge.cn/test/B/)

注意: 总时间限制: 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

#include<iostream>#include<iomanip>#include <string>using namespace std;class headquarter{    string headquarterName; //本部司令命名    int allLifeValue;       //所有生命值。int allLifeValue = 0;(不合适) https://stackoverflow.com/questions/27882915/cant-specify-explicit-initializer-for-arrays    int warriorValue[5];    //由给定的顺序和原始战士名字的数组,得到该总部战士生命值的数组    string warriorName[5];  //由给定的顺序和原始战士名字的数组,得到该总部战士名字的数组    int minWarriorValue;    //计算出战士中的最小生命值    int warriorNumber[5];   //本部每种武士的生产个数。此处 int warriorNumber[5] = { 0 } 错! https://stackoverflow.com/questions/27882915/cant-specify-explicit-initializer-for-arrays    int currentPosition;    //记录生产战士类别的当前位置    int totalNum;           //本部生产战士的总数public:    bool headstop;    //判断是否停止出兵,最好由构造函数初始化    static int time;  //武士出场时间    headquarter(string theheadquarterName, int theallLifeValue, const string thepriorallWarriorName[],         const int thewarriorValue[], const int thewarriorOrder[]);    void produceWarrior(int time, int position);  //生产战士。此处 void produceWarrior(int time, int position){};错!函数重复定义了    void dispatchWarrior();  //出兵};//初始化相关数据***int headquarter::time = 0;  //两个司令部共用同一个时间headquarter::headquarter(string theheadquarterName, int theallLifeValue, const string thepriorallWarriorName[],   //此处 static string thepriorallWarriorName[]错! https://stackoverflow.com/questions/26928614/static-variable-is-not-maintaing-its-scope/26928725#26928725    const int thewarriorValue[], const int thewarriorOrder[]):headquarterName(theheadquarterName),     allLifeValue(theallLifeValue), currentPosition(0), totalNum(0), headstop(false){    for (int i = 0; i < 5; i++){        warriorNumber[i] = 0;        warriorName[i] = thepriorallWarriorName[thewarriorOrder[i]]; //由给定的顺序和原始战士名字的数组,得到该总部战士名字的数组        warriorValue[i] = thewarriorValue[thewarriorOrder[i]];  //由给定的顺序和原始战士名字的数组,得到该总部战士生命值的数组    }    minWarriorValue = warriorValue[0];    for (int i = 1; i < 5; i++)    {        if (warriorValue[i] < minWarriorValue)            minWarriorValue = warriorValue[i];    }}//生产战士***void headquarter::produceWarrior(int time, int position){    totalNum++;    warriorNumber[position]++;    //按题目要求输出语句    cout << setfill('0') << setw(3) << time << ' ' << headquarterName << ' ' << warriorName[position]  //注意加上 #include <string> https://stackoverflow.com/questions/6320995/why-i-cannot-cout-a-string        << ' ' << totalNum << " born with strength " << warriorValue[position] << ',' << warriorNumber[position]        << ' ' << warriorName[position] << " in " << headquarterName << " headquarter" << endl;    allLifeValue -= warriorValue[position];}//出兵***void headquarter::dispatchWarrior(){    if (allLifeValue < minWarriorValue){        cout << setfill('0') << setw(3) << time << ' ' << headquarterName << " headquarter stops making warriors" << endl;        headstop = true;    }    else{        //从上面的判断句筛选后,现在一定能出兵        //从当前position开始增加,到某个位置出兵了停止        while (true){            if (allLifeValue >= warriorValue[currentPosition]){                produceWarrior(time, currentPosition);                currentPosition == 4 ? currentPosition = 0 : currentPosition++;                break;            }            else                currentPosition == 4 ? currentPosition = 0 : currentPosition++;        }    }}int main(){    int n = 0, m = 0;    string priorallWarriorName[5] = { "dragon", "ninja", "iceman", "lion", "wolf" }; //所有武士的姓名和原始排序     int warriorValue[5] = { 0 };  //依次是 dragon 、ninja、iceman、lion、wolf 的初始生命值    const int redwarriorOrder[5] = { 2, 3, 4, 1, 0 };  //红部出兵顺序    const int bluewarriorOrder[5] = { 3, 0, 1, 2, 4 }; //蓝部出兵顺序    cin >> n;    for (int i = 1; i <= n; i++){        cin >> m;        for (int j = 0; j < 5; j++)            cin >> warriorValue[j];        cout << "Case:" << i << endl;        headquarter redQuarter("red", m, priorallWarriorName, warriorValue, redwarriorOrder);        //headquarter redQuarter = headquarter("red", m, priorallWarriorName, warriorValue, redwarriorOrder); 这样也可以        headquarter blueQuarter("blue", m, priorallWarriorName, warriorValue, bluewarriorOrder);        //headquarter blueQuarter = headquarter("blue", m, priorallWarriorName, warriorValue, bluewarriorOrder); 这样也可以        headquarter::time = 0; //派兵次数置零,没有此句在多组测试数据下时间会一直累加!        while (!redQuarter.headstop || !blueQuarter.headstop){            if (!redQuarter.headstop)                redQuarter.dispatchWarrior();            if (!blueQuarter.headstop)                blueQuarter.dispatchWarrior();            headquarter::time++;  //redQuarter.time++;也可以        }    }    return 0;}

其它解法参考:
http://blog.csdn.net/nnnnnnnnnnnny/article/details/50277063

阅读全文
0 0
原创粉丝点击