程序設計實習MOOC/3129/B:魔獸世界之一:備戰

B:魔獸世界之一:備戰

總時間限制: 1000ms 內存限制: 65536kB
描述
魔獸世界的西面是紅魔軍的司令部,東面是藍魔軍的司令部。兩個司令部之間是依次排列的若干城市。
紅司令部,City 1,City 2,……,City n,藍司令部ios

兩軍的司令部都會製造武士。武士一共有 dragon 、ninja、iceman、lion、wolf 五種。每種武士都有編號、生命值、攻擊力這三種屬性。 數組

雙方的武士編號都是從1開始計算。紅方製造出來的第n個武士,編號就是n。一樣,藍方製造出來的第n個武士,編號也是n。 函數

武士在剛降生的時候有一個生命值。 測試

在每一個整點,雙方的司令部中各有一個武士降生。 spa

紅方司令部按照iceman、lion、wolf、ninja、dragon的順序循環制造武士。 code

藍方司令部按照lion、dragon、ninja、iceman、wolf的順序循環制造武士。 事件

製造武士須要生命元。 內存

製造一個初始生命值爲m的武士,司令部中的生命元就要減小m個。 ci

若是司令部中的生命元不足以製造某個按順序應該製造的武士,那麼司令部就試圖製造下一個。若是全部武士都不能製造了,則司令部中止製造武士。字符串

給定一個時間,和雙方司令部的初始生命元數目,要求你將從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

題解分析

我定義了兩個類:武士類wushi和司令營類headquarter。

武士類wushi

武士wushi類有六個靜態成員:
1.字符串數組wushiName[5]存放五種武士的種類名稱,經過輸入獲取
2.紅魔武士的建造順序,存放在整型數組redWushiSeq[5]中,爲固定值
3.藍魔武士的建造順序,存放在整型數組redWushiSeq[5]中,爲固定值
4.用一個整型數組wushiNum[12]存放不一樣種類的武士的數量。下標爲0-5的六個元素存放的是紅魔武士的數量,下標爲5的元素存放紅魔武士的總數量,即紅魔武士編號。下標爲6-11的六個元素存放的是藍魔武士的數量,下標爲11的元素存放藍魔武士的總數量,即紅魔武士編號。
5.武士的生命值數組wushiLife[5],存放不一樣種類武士的生命值,經過輸入獲取
6.最小武士的生命值minWushiLife,int類型

武士類還有兩個普通成員變量:
1.整型變量headquarterType表明該武士屬於哪一個司令營,0表示紅魔武士,6表示藍魔武士
2.整型變量type表示武士種類,好比0表明dragon武士(0 dragon 、1 ninja、2 iceman、3 lion、4 wolf)
經過這兩個普通成員變量headquarterTypetype就能夠知道該武士是哪一個司令營的何種武士,好比headquarterType==0而且type==0則表示該武士是紅魔司令營的dragon武士。
武士類構造函數:武士wushi類的構造函數中會執行對應種類type的武士數量增一的操做。
武士類還有兩個普通成員函數getNo()getWushiNum(),一個靜態成員函數minLife()getNo()返回武士的編號,getWushiNum()返回該種武士在所在的司令營中的數量,minLife()返回最小武士生命值。

武士類wushi代碼

class wushi
{
public:
    static string   wushiName[5];                   //武士種類名稱
    static int      redWushiSeq[5];                 //紅魔武士順序
    static int      blueWushiSeq[5];                //藍魔武士順序
    static int      wushiNum[12];                   //武士數量 0-5紅 6-11藍
    static int      wushiLife[5];                   //武士的生命值數組
    static int      minWushiLife;                   //武士生命值的最小值
    int             headquarterType;                //司令營 0紅 6藍
    int             type;                           //武士種類 0 dragon 、1 ninja、2 iceman、3 lion、4 wolf
    wushi(int t, int h): type(t), headquarterType(h)
    {
        wushiNum[h + 5]++;                          //總數
        wushiNum[h + t]++;                          //對應武士種類
    }
    /** 獲取編號值 */
    int getNo()
    {
        return wushiNum[headquarterType + 5];
    }
    /** 獲取當前武士種類的數量 */
    int getWushiNum()
    {
        return wushiNum[headquarterType + type];
    }
    /** 計算最小武士生命值並返回 */
    static int minLife();
};
int wushi::minLife()
{
    int minL = wushiLife[0];
    for(int i = 1; i < 5; i++)
    {
        if(minL > wushiLife[i])
        {
            minL = wushiLife[i];
        }
    }
    return minL;
}

司令營類headquarter

司令營類headquarter有一個靜態成員變量totaLife表示總生命元。
司令營類headquarter有四個普通成員變量:
1.司令營的類型headquarterType,標識是紅魔司令營仍是藍魔司令營,int型,0表示紅,6表示藍
2.司令營的剩餘生命元life,int
3.司令營是否中止建造武士isStop,bool
4.當前準備建造的武士的循環次序號seq (初始時爲0,建造司令營中的第一個武士則次序號seq爲0,建造第二個次序號seq爲1,建造第三個次序號seq爲2,...,第五個次序號seq爲0,依次循環下去)
司令營類headquarter的構造函數須要指定司令營的類型,肯定是紅魔司令營仍是藍魔司令營。司令營剩餘生命值life初始化爲總生命元totaLifeisStopfalseseq初始值爲0

司令營類有兩個普通成員函數bulidWushi()stopBulid()bulidWushi()在指定的時刻建造武士,stopBulid()在指定時刻中止建造武士的任務。

司令營類headquarter代碼

class headquarter
{
public:
    static int  totaLife;                           //總生命元
    int         headquarterType;                    //司令營 0紅 6藍
    int         life;                               //剩餘生命元
    bool        isStop;                             //是否中止建造武士
    int         seq;                                //當前建造武士次序號
    headquarter(int hT): headquarterType(hT), life(totaLife), isStop(false), seq(0) {}
    void bulidWushi(int time, wushi bwushi);
    void stopBulid(int time);
};
void headquarter::bulidWushi(int time, wushi bwushi)
{
    cout.fill('0');                             //設置填充字符
    cout.width(3);                              //設置域寬
    cout << time;
    cout << (headquarterType == RED ? " red " : " blue ")
         << wushi::wushiName[bwushi.type]
         << " "
         << bwushi.getNo()
         << " born with strength "
         << wushi::wushiLife[bwushi.type]
         << ","
         << bwushi.getWushiNum()
         << " "
         << wushi::wushiName[bwushi.type]
         << " in"
         << (headquarterType == RED ? " red " : " blue ")
         << "headquarter"
         << endl;
    life -= wushi::wushiLife[bwushi.type];
    seq = (seq + 1) % 5;                        //下一個
}
void headquarter::stopBulid(int time)
{
    isStop = true;
    cout.fill('0');                             //設置填充字符
    cout.width(3);                              //設置域寬
    cout << time;
    cout << (headquarterType == RED ? " red " : " blue ")
         << "headquarter stops making warriors"
         << endl;
}

輸出時刻time的格式爲三個字符寬度,不足三位則填充「0」,代碼以下

cout.fill('0');                             //設置填充字符
    cout.width(3);                              //設置域寬
    cout << time;

靜態成員要在全部函數外面單獨聲明

/** 靜態成員聲明 */
string  wushi::wushiName[]      = {"dragon", "ninja", "iceman", "lion", "wolf"};    //武士種類名稱
int     wushi::redWushiSeq[]    = {2, 3, 4, 1, 0};                                  //紅魔武士順序
int     wushi::blueWushiSeq[]   = {3, 0, 1, 2, 4};                                  //藍魔武士順序
int     wushi::wushiNum[]       = {0};                                              //武士數量 0-5紅 6-11藍
int     wushi::minWushiLife     = 0;                                                //武士生命值的最小值
int     headquarter::totaLife;                                                      //司令營的總生命元
int     wushi::wushiLife[5];                                                        //各類武士的生命值

完整代碼

#include<iostream>
#include<string.h>
#include<cstdio>
using namespace std;
const int RED   = 0;
const int BLUE  = 6;
class wushi
{
public:
    static string   wushiName[5];                   //武士種類名稱
    static int      redWushiSeq[5];                 //紅魔武士順序
    static int      blueWushiSeq[5];                //藍魔武士順序
    static int      wushiNum[12];                   //武士數量 0-5紅 6-11藍
    static int      wushiLife[5];                   //武士的生命值數組
    static int      minWushiLife;                   //武士生命值的最小值
    int             headquarterType;                //司令營 0紅 6藍
    int             type;                           //武士種類 0 dragon 、1 ninja、2 iceman、3 lion、4 wolf
    wushi(int t, int h): type(t), headquarterType(h)
    {
        wushiNum[h + 5]++;                          //總數
        wushiNum[h + t]++;                          //對應武士種類
    }
    /** 獲取編號值 */
    int getNo()
    {
        return wushiNum[headquarterType + 5];
    }
    /** 獲取當前武士種類的數量 */
    int getWushiNum()
    {
        return wushiNum[headquarterType + type];
    }
    /** 計算最小武士生命值並返回 */
    static int minLife();
};
int wushi::minLife()
{
    int minL = wushiLife[0];
    for(int i = 1; i < 5; i++)
    {
        if(minL > wushiLife[i])
        {
            minL = wushiLife[i];
        }
    }
    return minL;
}
class headquarter
{
public:
    static int  totaLife;                           //總生命元
    int         headquarterType;                    //司令營 0紅 6藍
    int         life;                               //剩餘生命元
    bool        isStop;                             //是否中止建造武士
    int         seq;                                //當前建造武士次序號
    headquarter(int hT): headquarterType(hT), life(totaLife), isStop(false), seq(0) {}
    void bulidWushi(int time, wushi bwushi);
    void stopBulid(int time);
};
void headquarter::bulidWushi(int time, wushi bwushi)
{
    cout.fill('0');                             //設置填充字符
    cout.width(3);                              //設置域寬
    cout << time;
    cout << (headquarterType == RED ? " red " : " blue ")
         << wushi::wushiName[bwushi.type]
         << " "
         << bwushi.getNo()
         << " born with strength "
         << wushi::wushiLife[bwushi.type]
         << ","
         << bwushi.getWushiNum()
         << " "
         << wushi::wushiName[bwushi.type]
         << " in"
         << (headquarterType == RED ? " red " : " blue ")
         << "headquarter"
         << endl;
    life -= wushi::wushiLife[bwushi.type];
    seq = (seq + 1) % 5;                        //下一個
}
void headquarter::stopBulid(int time)
{
    isStop = true;
    cout.fill('0');                             //設置填充字符
    cout.width(3);                              //設置域寬
    cout << time;
    cout << (headquarterType == RED ? " red " : " blue ")
         << "headquarter stops making warriors"
         << endl;
}
/** 靜態成員聲明 */
string  wushi::wushiName[]      = {"dragon", "ninja", "iceman", "lion", "wolf"};    //武士種類名稱
int     wushi::redWushiSeq[]    = {2, 3, 4, 1, 0};                                  //紅魔武士順序
int     wushi::blueWushiSeq[]   = {3, 0, 1, 2, 4};                                  //藍魔武士順序
int     wushi::wushiNum[]       = {0};                                              //武士數量 0-5紅 6-11藍
int     wushi::minWushiLife     = 0;                                                //武士生命值的最小值
int     headquarter::totaLife;                                                      //司令營的總生命元
int     wushi::wushiLife[5];                                                        //各類武士的生命值
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("mout.txt","w",stdout);
    int testn;                  //測試數據組數
    int casen   = 0;            //當前測試組數
    int time    = 0;            //時刻
    cin >> testn;               //輸入測試數據組數
    while(testn > casen)
    {
        time = 0;
        casen++;                //當前測試數據組
        memset(wushi::wushiNum, 0, sizeof(int) * 12);
        /* 輸入總生命元和武士生命值 */
        cin >> headquarter::totaLife
            >> wushi::wushiLife[0]
            >> wushi::wushiLife[1]
            >> wushi::wushiLife[2]
            >> wushi::wushiLife[3]
            >> wushi::wushiLife[4];
        cout << "Case:" << casen << endl;                   //輸出當前測試數據組
        headquarter redH(RED);                              //紅魔司令營
        headquarter blueH(BLUE);                            //藍魔司令營
        wushi::minWushiLife = wushi::minLife();             //求出武士生命最小值
        while(!redH.isStop || !blueH.isStop)                //紅魔或者藍魔有一方尚未中止建造則繼續建造
        {
            if(!redH.isStop)                                //紅魔尚未中止建造
            {
                int wType = wushi::redWushiSeq[redH.seq];   //當前準備建造的武士種類
                if(redH.life >= wushi::wushiLife[wType])
                {
                    /* 造武士 */
                    wushi wRed(wType, RED);
                    redH.bulidWushi(time, wRed);
                }
                else if(redH.life >= wushi::minWushiLife)
                {
                    /* 當前生命元不足,造下一個武士 */
                    redH.seq = (redH.seq + 1) % 5;          //下一個
                    wType = wushi::redWushiSeq[redH.seq];
                    while(redH.life < wushi::wushiLife[wType])
                    {
                        redH.seq = (redH.seq + 1) % 5;      //下一個
                        wType = wushi::redWushiSeq[redH.seq];
                    }
                    wushi wRed(wType, RED);
                    redH.bulidWushi(time, wRed);
                }
                else
                {
                    /* 中止建造 */
                    redH.stopBulid(time);
                }
            }
            if(!blueH.isStop)                               //藍魔尚未中止建造
            {
                int wType = wushi::blueWushiSeq[blueH.seq]; //當前準備建造的武士種類
                if(blueH.life >= wushi::wushiLife[wType])
                {
                    /* 造武士 */
                    wushi wBlue(wType, BLUE);
                    blueH.bulidWushi(time, wBlue);
                }
                else if(blueH.life >= wushi::minWushiLife)
                {
                    /* 當前生命元不足,造下一個武士 */
                    blueH.seq = (blueH.seq + 1) % 5;        //下一個
                    wType = wushi::blueWushiSeq[blueH.seq];
                    while(blueH.life < wushi::wushiLife[wType])
                    {
                        //cout<<"藍魔生命:"<<blueH.life<<"下個武士生命:"<<wushi::wushiLife[wType]<<endl;
                        blueH.seq = (blueH.seq + 1) % 5;    //下一個
                        wType = wushi::blueWushiSeq[blueH.seq];
                    }
                    wushi wBlue(wType, BLUE);
                    blueH.bulidWushi(time, wBlue);
                }
                else
                {
                    /* 中止建造 */
                    blueH.stopBulid(time);
                }
            }
            time++;                                         //下一時刻
        }
    }
}

第一次寫的完整代碼

#include<iostream>
#include<string.h>
#include<cstdio>
using namespace std;
class wushiRed
{
public:
    static int wushiNum[6];
    int type;       //武士種類 0 dragon 、1 ninja、2 iceman、3 lion、4 wolf
    int no;         //編號
    int life;       //生命值
    int gong;       //攻擊力
    wushiRed(int t, int n, int l, int g = 0): type(t), no(n + 1), life(l), gong(g)
    {
        wushiNum[5]++;//總數
        wushiNum[t]++;//對應武士種類

    }
};
class wushiBlue
{
public:
    int type;       //武士種類 0 dragon 、1 ninja、2 iceman、3 lion、4 wolf
    int no;         //編號
    int life;       //生命值
    int gong;       //攻擊力
    static int wushiNum[6];
    wushiBlue(int t, int n, int l, int g = 0): type(t), no(n + 1), life(l), gong(g)
    {
        wushiNum[5]++;//總數
        wushiNum[t]++;//對應武士種類

    }
};
int minLife(int array[], int n)
{
    int min = array[0];
    for(int i = 1; i < n; i++)
    {
        if(min > array[i])
        {
            min = array[i];
        }
    }
    return min;
}
int wushiRed::wushiNum[] = {0};
int wushiBlue::wushiNum[] = {0};
int main()
{
    //freopen("in.txt","r",stdin);
    int testn;              //測試數據組數
    int casen = 0;          //目前測試組數
    int totalLife;          //總的生命元
    int redLife;            //紅魔生命元
    int blueLife;           //藍魔生命元
    int redWushi = 0;           //當前建造紅魔武士次序號
    int blueWushi = 0;          //當前建造藍魔武士次序號
    string wushiName[] = {"dragon", "ninja", "iceman", "lion", "wolf"};
    int redWushiSeq[] = {2, 3, 4, 1, 0};//紅魔武士順序
    int blueWushiSeq[] = {3, 0, 1, 2, 4};//藍魔武士順序
    int wushiLife[5];       //武士的生命值數組
    bool redStop = false;   //紅魔是否中止
    bool blueStop = false;  //藍魔是否中止
    int minWushiLife = 0;   //武士生命值的最小值
    int time = 0;           //時刻
    cin >> testn;           //輸入測試數據組數
    while(testn > casen)
    {
        memset(wushiRed::wushiNum, 0, sizeof(int) * 6);
        memset(wushiBlue::wushiNum, 0, sizeof(int) * 6);
        time = 0;
        //輸入總生命元和武士生命值
        cin >> totalLife >> wushiLife[0] >> wushiLife[1] >> wushiLife[2] >> wushiLife[3] >> wushiLife[4];
        casen++;                                //當前測試數據組
        cout << "Case:" << casen << endl;       //輸出當前測試數據組
        minWushiLife = minLife(wushiLife, 5);   //求出武士生命最小值
        //cout<<"最小武士生命:"<<minWushiLife<<endl;
        redLife = totalLife;                    //紅魔目前的生命元
        blueLife = totalLife;                   //藍魔目前的生命元
        redWushi = 0;                           //紅魔循環建造武士次序號
        blueWushi = 0;                          //藍魔循環建造武士次序號
        redStop = false;
        blueStop = false;
        while(!redStop || !blueStop)            //紅魔或者藍魔有一方尚未中止建造
        {
            if(!redStop && redLife >= 0)         //紅魔還有生命元而且沒有中止建造
            {
                int wType = redWushiSeq[redWushi];//當前準備建造的武士種類
                //cout<<casen<<" 紅魔剩餘生命:"<<redLife <<"當前武士生命:"<<wushiLife[wType]<<"最小武士生命:"<<minWushiLife<<endl;
                if(redLife >= wushiLife[wType])
                {
                    wushiRed wRed(wType, wushiRed::wushiNum[5], wushiLife[wType]);
                    //cout << "004 blue lion 5 born with strength 5,2 lion in red headquarter" << endl;
                    cout.fill('0');//設置填充字符
                    cout.width(3);//設置域寬
                    cout << time;
                    cout << " red " << wushiName[wType]  << " " << wRed.no << " born with strength " << wushiLife[wType] << "," << wushiRed::wushiNum[wType] << " " << wushiName[wType] << " in red headquarter" << endl;
                    redLife -= wushiLife[wType];
                    redWushi = (redWushi + 1) % 5;//下一個
                }
                else if(redLife >= minWushiLife)
                {
                    for(int i = 0 ; i < 5; i++)
                    {
                        redWushi = (redWushi + 1) % 5;//下一個
                        wType = redWushiSeq[redWushi];
                        if(redLife >= wushiLife[wType])//造武士
                        {
                            wushiRed wRed(wType, wushiRed::wushiNum[5], wushiLife[wType]);
                            //cout << "004 blue lion 5 born with strength 5,2 lion in red headquarter" << endl;
                            cout.fill('0');//設置填充字符
                            cout.width(3);//設置域寬
                            cout << time;
                            cout <<  " red " << wushiName[wType] << " " << wRed.no << " born with strength " << wushiLife[wType] << "," << wushiRed::wushiNum[wType] << " " << wushiName[wType] << " in red headquarter" << endl;
                            redLife -= wushiLife[wType];
                            redWushi = (redWushi + 1) % 5;//下一個
                            break;
                        }
                    }
                }
                else
                {
                    redStop = true;
                    cout.fill('0');//設置填充字符
                    cout.width(3);//設置域寬
                    cout << time;
                    cout << " red headquarter stops making warriors" << endl;
                }
            }

            if(!blueStop && blueLife >= 0)         //藍魔還有生命元而且沒有中止建造
            {
                int wType = blueWushiSeq[blueWushi];//當前準備建造的武士種類
                //cout<<"藍魔剩餘生命:"<<blueLife <<"當前武士生命:"<<wushiLife[wType]<<"最小武士生命:"<<minWushiLife<<endl;
                if(blueLife >= wushiLife[wType])
                {
                    wushiBlue wBlue(wType, wushiBlue::wushiNum[5], wushiLife[wType]);
                    //cout << "004 blue lion 5 born with strength 5,2 lion in red headquarter" << endl;
                    cout.fill('0');//設置填充字符
                    cout.width(3);//設置域寬
                    cout << time;
                    cout << " blue " << wushiName[wType] << " " << wBlue.no << " born with strength " << wushiLife[wType] << "," << wushiBlue::wushiNum[wType] <<  " " << wushiName[wType] << " in blue headquarter" << endl;
                    blueLife -= wushiLife[wType];
                    blueWushi = (blueWushi + 1) % 5;
                }
                else if(blueLife >= minWushiLife)
                {
                    for(int i = 0 ; i < 5; i++)
                    {
                        blueWushi = (blueWushi + 1) % 5;
                        wType = blueWushiSeq[blueWushi];
                        if(blueLife >= wushiLife[wType])//造武士
                        {
                            wushiBlue wBlue(wType, wushiBlue::wushiNum[5], wushiLife[wType]);
                            //cout << "004 blue lion 5 born with strength 5,2 lion in red headquarter" << endl;
                            cout.fill('0');//設置填充字符
                            cout.width(3);//設置域寬
                            cout << time;
                            cout   << " blue " << wushiName[wType]  << " " << wBlue.no << " born with strength " << wushiLife[wType] << "," << wushiBlue::wushiNum[wType] << " " << wushiName[wType] << " in blue headquarter" << endl;
                            blueLife -= wushiLife[wType];
                            blueWushi = (blueWushi + 1) % 5;
                            break;
                        }
                    }
                }
                else
                {
                    blueStop = true;
                    cout.fill('0');//設置填充字符
                    cout.width(3);//設置域寬
                    cout << time;
                    cout  << " blue headquarter stops making warriors" << endl;
                }
            }
            time++;//下一時刻
        }
    }
}
相關文章
相關標籤/搜索