編程題#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 }
