1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
| #include <iostream> #include <string> #include <iomanip> #include <ctime> using namespace std; const int WARRIOR_NUM = 5; const int WEAPON_NUM = 2; enum warrior{total=0, dragon, ninja, iceman, lion, wolf};
class Warrior{ protected: int No; int HP; short WeaponHP[WEAPON_NUM+1]; string Name; public: friend class Command; virtual string CreateWeapon(int No); };
string Warrior::CreateWeapon(int _No) { _No = _No % 3; switch(_No) { case 0: WeaponHP[0]=1; return "sword"; break; case 1: WeaponHP[1]=1; return "bomb"; break; case 2: WeaponHP[2]=1; return "arrow"; break; default: break; } }
class Dragon:public Warrior{ private: double Morale; public: Dragon(int _No, const int _HP, string belong, const double CommandHP, int WarriorKindNumber); };
Dragon::Dragon(int _No, const int _HP, string belong, const double CommandHP, int WarriorKindNumber) { No=_No; HP=_HP; Name="dragon"; Morale = CommandHP/HP; cout<<belong<<" "<<this->Name<<" "<<this->No<<" born with strength "<<this->HP<<", "; cout<<WarriorKindNumber<<" "<<this->Name<<" in "<<belong<<" headquarter, "; cout<<"rest HP = "<<(int)CommandHP<<endl; cout<<"It has a "<<this->CreateWeapon(No)<<", and it's morale is "<<setiosflags(ios::fixed)<<setprecision(2)<<Morale<<endl; }
class Ninja:public Warrior{ public: Ninja(int _No, const int _HP, string belong, const int CommandHP, int WarriorKindNumber); };
Ninja::Ninja(int _No, const int _HP, string belong, const int CommandHP, int WarriorKindNumber) { No=_No; HP=_HP; Name="ninja"; cout<<belong<<" "<<this->Name<<" "<<this->No<<" born with strength "<<this->HP<<", "; cout<<WarriorKindNumber<<" "<<this->Name<<" in "<<belong<<" headquarter, "; cout<<"rest HP = "<<CommandHP<<endl; cout<<"It has a "<<this->CreateWeapon(No)<<" and a "<<this->CreateWeapon(No+1)<<endl; }
class Iceman:public Warrior{ public: Iceman(int _No, const int _HP, string belong, const int CommandHP, int WarriorKindNumber); };
Iceman::Iceman(int _No, const int _HP, string belong, const int CommandHP, int WarriorKindNumber) { No=_No; HP=_HP; Name="iceman"; cout<<belong<<" "<<this->Name<<" "<<this->No<<" born with strength "<<this->HP<<", "; cout<<WarriorKindNumber<<" "<<this->Name<<" in "<<belong<<" headquarter, "; cout<<"rest HP = "<<CommandHP<<endl; cout<<"It has a "<<this->CreateWeapon(No)<<endl; }
class Lion:public Warrior{ private: int Loyalty; public: Lion(int _No, const int _HP, string belong, const int CommandHP, int WarriorKindNumber); };
Lion::Lion(int _No, const int _HP, string belong, const int CommandHP, int WarriorKindNumber) { No=_No; HP=_HP; Name="lion"; Loyalty=CommandHP; cout<<belong<<" "<<this->Name<<" "<<this->No<<" born with strength "<<this->HP<<", "; cout<<WarriorKindNumber<<" "<<this->Name<<" in "<<belong<<" headquarter, "; cout<<"rest HP = "<<CommandHP<<endl; cout<<"It's loyalty is "<<Loyalty<<endl; }
class Wolf:public Warrior{ public: Wolf(int _No, const int _HP, string belong, const int CommandHP, int WarriorKindNumber); };
Wolf::Wolf(int _No, const int _HP, string belong, const int CommandHP, int WarriorKindNumber) { No=_No; HP=_HP; Name="wolf"; cout<<belong<<" "<<this->Name<<" "<<this->No<<" born with strength "<<this->HP<<", "; cout<<WarriorKindNumber<<" "<<this->Name<<" in "<<belong<<" headquarter, "; cout<<"rest HP = "<<CommandHP<<endl; }
class Command{ private: string Name; short OrderCount; int HP; int WarriorNumber[WARRIOR_NUM+1]; Warrior *myWarrior[200]; public: static int time; short Order[WARRIOR_NUM+1]; Command(string _Name, const short _Order[], const int _HP); ~Command(); bool CreateWarrior(short p[]); void StopCreateWarrior(); }; int Command::time = -1;
Command::Command(string _Name, const short _Order[], const int _HP):Name(_Name), HP(_HP) { OrderCount = 1; for(int i=0; i<=WARRIOR_NUM; i++) { Order[i] = _Order[i]; WarriorNumber[i] = 0; } }
Command::~Command() { while(WarriorNumber[total]) { delete myWarrior[WarriorNumber[total]]; WarriorNumber[total]--; } }
bool Command::CreateWarrior(short p[]) { if( HP<p[1] && HP<p[2] && HP<p[3] && HP<p[4] && HP<p[5] ) return 0; while(HP < p[Order[OrderCount]]) { OrderCount++; if(OrderCount==WARRIOR_NUM+1) OrderCount=1; } short ord = Order[OrderCount]; WarriorNumber[total]++; WarriorNumber[ord]++; HP -= p[ord]; short No = WarriorNumber[total]; cout<<setw(3)<<setfill('0')<<time<<" "; switch(ord) { case 1: myWarrior[No]=new Dragon(No, p[1], Name, (double)HP, WarriorNumber[ord]); break; case 2: myWarrior[No]=new Ninja(No, p[2], Name, HP, WarriorNumber[ord]); break; case 3: myWarrior[No]=new Iceman(No, p[3], Name, HP, WarriorNumber[ord]); break; case 4: myWarrior[No]=new Lion(No, p[4], Name, HP, WarriorNumber[ord]); break; case 5: myWarrior[No]=new Wolf(No, p[5], Name, HP, WarriorNumber[ord]); break; default: break; } OrderCount++; if(OrderCount == WARRIOR_NUM+1) OrderCount=1; return 1; }
void Command::StopCreateWarrior() { cout<<setw(3)<<setfill('0')<<time<<" "<<this->Name<<" headquarter stops making warriors"<<endl; }
int main() { int n=1; short redOrder[WARRIOR_NUM+1]={0, iceman, lion, wolf, ninja, dragon}; short blueOrder[WARRIOR_NUM+1]={0, lion, dragon, ninja, iceman, wolf}; short M, dm, nm, im, lm, wm; clock_t startTime, endTime; while(n--) { cin>>M; cin>>dm>>nm>>im>>lm>>wm; startTime = clock(); Command red("red", redOrder, M), blue("blue", blueOrder, M); short p[WARRIOR_NUM+1] = {0, dm, nm, im, lm, wm}; bool redCreate = 1, blueCreate = 1; Command::time = -1; while(redCreate || blueCreate) { Command::time ++; if(redCreate == 1 && red.CreateWarrior(p) == 0){redCreate = 0; red.StopCreateWarrior();} if(blueCreate == 1 && blue.CreateWarrior(p) == 0){blueCreate = 0; blue.StopCreateWarrior();} cout<<endl; } } endTime = clock(); cout<<"time:"<<(double)(endTime - startTime) <<"ms"<<endl; return 0; }
|