P大魔兽大作业(二)
和(一)差不多,不懂得可以看我上一篇随笔......
算法上没什么特殊的地方,直接贴代码。
#include<iostream> #include<cstring> #include<iomanip> #include<cmath> #include<cstdio> #include<cstdlib> using namespace std; class Redhead { public: int RMlife;//定义生命元 int Riceman; int Rlion; int Rwolf; int Rninja; int Rdragon;//定义红魔军司令部各个武士的数量 char Rs[5][20] = { "iceman","lion","wolf","ninja","dragon" }; char wuqi[3][20] = { "sword","bomb","arrow" }; Redhead(int i) :RMlife(i) { Riceman = 0; Rlion = 0; Rwolf = 0; Rninja = 0; Rdragon = 0; } Redhead() { } void Rtozero() { Riceman = 0; Rlion = 0; Rwolf = 0; Rninja = 0; Rdragon = 0; RMlife = 0; } }; class Bluehead { public: int BMlife;//定义生命元 int Biceman; int Blion; int Bwolf; int Bninja; int Bdragon;//定义蓝魔军司令部各个武士的数量 char Bs[5][20] = { "lion","dragon","ninja","iceman","wolf" }; char wuqi[3][20] = { "sword","bomb","arrow" }; Bluehead(int i) :BMlife(i) { Biceman = 0; Blion = 0; Bwolf = 0; Bninja = 0; Bdragon = 0; } Bluehead() { } void Btozero() { Biceman = 0; Blion = 0; Bwolf = 0; Bninja = 0; Bdragon = 0; BMlife = 0; } }; void r1(); void r2(); void r3(); void r4(); void r5(); void b1(); void b2(); void b3(); void b4(); void b5(); void Red1(); void Red2(); void Red3(); void Red4(); void Red5(); void Blue1(); void Blue2(); void Blue3(); void Blue4(); void Blue5(); int M; Redhead Cred; Bluehead Cblue; int Miceman; int Mlion; int Mwolf; int Mninja; int Mdragon; int Flag = 0; int BFlag = 0; int rflag = 1; int bflag = 1; int i; int main()//--------------------------------------------------------------------------------------// { int Rv = 0; int Bv = 0; int Ntest; cin >> Ntest; for (int NTEST = 1; NTEST <= Ntest; NTEST++) { cout << "Case:" << NTEST << endl; int M; cin >> M; cin >> Mdragon >> Mninja >> Miceman >> Mlion >> Mwolf; Cred = M; Cblue = M; for (i = 0;; i++) { if (rflag == 1) { Red1(); } else { if (rflag == 2) { Red2(); } else { if (rflag == 3) { Red3(); } else { if (rflag == 4) { Red4(); } else { if (rflag == 5) { Red5(); } } } } } if (Flag == 0 && Rv == 0) { Rv = 1; cout << setfill('0') << setw(3) << i << " red headquarter stops making warriors" << endl; } else { Flag = 0; } //-----// if (bflag == 1) { Blue1(); } else { if (bflag == 2) { Blue2(); } else { if (bflag == 3) { Blue3(); } else { if (bflag == 4) { Blue4(); } else { if (bflag == 5) { Blue5(); } } } } } if (BFlag == 0 && Bv == 0) { Bv = 1; cout << setfill('0') << setw(3) << i << " blue headquarter stops making warriors" << endl; } else { BFlag = 0; } if (Bv == 1 && Rv == 1) { break; } } Bv = 0; Rv = 0; Cred.Rtozero(); Cblue.Btozero(); Flag = 0; BFlag = 0; rflag = 1; bflag = 1; } return 0; } //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------// void r1() { Cred.RMlife -= Miceman; Cred.Riceman++; cout << setfill('0') << setw(3) << i; cout << " "; cout << "red " << "iceman" << " " << i + 1 << " born with strength " << Miceman << "," << Cred.Riceman << " iceman in red headquarter" << endl; cout << "It has a " << Cred.wuqi[(i + 1) % 3] << endl; Flag = 1; rflag = 2; } void r2() { Cred.RMlife -= Mlion; Cred.Rlion++; cout << setfill('0') << setw(3) << i; cout << " "; cout << "red " << "lion" << " " << i + 1 << " born with strength " << Mlion << "," << Cred.Rlion << " lion in red headquarter" << endl; cout << "It's loyalty is " << Cred.RMlife << endl; Flag = 1; rflag = 3; } void r3() { Cred.RMlife -= Mwolf; Cred.Rwolf++; cout << setfill('0') << setw(3) << i; cout << " "; cout << "red " << "wolf" << " " << i + 1 << " born with strength " << Mwolf << "," << Cred.Rwolf << " wolf in red headquarter" << endl; Flag = 1; rflag = 4; } void r4() { Cred.RMlife -= Mninja; Cred.Rninja++; cout << setfill('0') << setw(3) << i; cout << " "; cout << "red " << "ninja" << " " << i + 1 << " born with strength " << Mninja << "," << Cred.Rninja << " ninja in red headquarter" << endl; cout << "It has a " << Cred.wuqi[(i + 1) % 3] << " and a " << Cred.wuqi[(i + 2) % 3] << endl; Flag = 1; rflag = 5; } void r5() { double shiqi = 0; Cred.RMlife -= Mdragon; Cred.Rdragon++; shiqi = (double)Cred.RMlife / Mdragon; cout << setfill('0') << setw(3) << i; cout << " "; cout << "red " << "dragon" << " " << i + 1 << " born with strength " << Mdragon << "," << Cred.Rdragon << " dragon in red headquarter" << endl; cout << "It has a " << Cred.wuqi[(i + 1) % 3] << ",and it's morale is " << setiosflags(ios::fixed) << setprecision(2) << shiqi << endl; Flag = 1; rflag = 1; } void Red1() { if (Cred.RMlife - Miceman >= 0) {r1();} else { if (Cred.RMlife - Mlion >= 0) {r2();} else { if (Cred.RMlife - Mwolf >= 0) {r3();} else { if (Cred.RMlife - Mninja >= 0) {r4();} else { if (Cred.RMlife - Mdragon >= 0) {r5();} } } } } } void Red2() { if (Cred.RMlife - Mlion >= 0) {r2();} else { if (Cred.RMlife - Mwolf >= 0) {r3();} else { if (Cred.RMlife - Mninja >= 0) {r4();} else { if (Cred.RMlife - Mdragon >= 0) {r5();} else { if (Cred.RMlife - Miceman >= 0) {r1();} } } } } } void Red3() { if (Cred.RMlife - Mwolf >= 0) {r3();} else { if (Cred.RMlife - Mninja >= 0) { r4(); } else { if (Cred.RMlife - Mdragon >= 0) { r5(); } else { if (Cred.RMlife - Miceman >= 0) { r1(); } else { if (Cred.RMlife - Mlion >= 0) { r2(); } } } } } } void Red4() { if (Cred.RMlife - Mninja >= 0) { r4(); } else { if (Cred.RMlife - Mdragon >= 0) { r5(); } else { if (Cred.RMlife - Miceman >= 0) { r1(); } else { if (Cred.RMlife - Mlion >= 0) { r2(); } else { if (Cred.RMlife - Mwolf >= 0) { r3(); } } } } } } void Red5() { if (Cred.RMlife - Mdragon >= 0) { r5(); } else { if (Cred.RMlife - Miceman >= 0) { r1(); } else { if (Cred.RMlife - Mlion >= 0) { r2(); } else { if (Cred.RMlife - Mwolf >= 0) { r3(); } else { if (Cred.RMlife - Mninja >= 0) { r4(); } } } } } }//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------// //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------blue// void b1() { Cblue.BMlife -= Mlion; Cblue.Blion++; cout << setfill('0') << setw(3) << i; cout << " "; cout << "blue " << "lion" << " " << i + 1 << " born with strength " << Mlion << "," << Cblue.Blion << " lion in blue headquarter" << endl; cout << "It's loyalty is " << Cblue.BMlife << endl; BFlag = 1; bflag = 2; } void b2() { Cblue.BMlife -= Mdragon; Cblue.Bdragon++; double shiqi = 0; shiqi = (double)Cblue.BMlife / Mdragon; cout << setfill('0') << setw(3) << i; cout << " "; cout << "blue " << "dragon" << " " << i + 1 << " born with strength " << Mdragon << "," << Cblue.Bdragon << " dragon in blue headquarter" << endl; cout << "It has a " << Cblue.wuqi[(i + 1) % 3] << ",and it's morale is " << setiosflags(ios::fixed) << setprecision(2) << shiqi << endl; BFlag = 1; bflag = 3; } void b3() { Cblue.BMlife -= Mninja; Cblue.Bninja++; cout << setfill('0') << setw(3) << i; cout << " "; cout << "blue " << "ninja" << " " << i + 1 << " born with strength " << Mninja << "," << Cblue.Bninja << " ninja in blue headquarter" << endl; cout << "It has a " << Cblue.wuqi[(i + 1) % 3] << " and a " << Cblue.wuqi[(i + 2) % 3] << endl; BFlag = 1; bflag = 4; } void b4() { Cblue.BMlife -= Miceman; Cblue.Biceman++; cout << setfill('0') << setw(3) << i; cout << " "; cout << "blue " << "iceman" << " " << i + 1 << " born with strength " << Miceman << "," << Cblue.Biceman << " iceman in blue headquarter" << endl; cout << "It has a " << Cblue.wuqi[(i + 1) % 3] << endl; BFlag = 1; bflag = 5; } void b5() { Cblue.BMlife -= Mwolf; Cblue.Bwolf++; cout << setfill('0') << setw(3) << i; cout << " "; cout << "blue " << "wolf" << " " << i + 1 << " born with strength " << Mwolf << "," << Cblue.Bwolf << " wolf in blue headquarter" << endl; BFlag = 1; bflag = 1; } void Blue1() { if (Cblue.BMlife - Mlion >= 0) { b1(); } else { if (Cblue.BMlife - Mdragon >= 0) { b2(); } else { if (Cblue.BMlife - Mninja >= 0) { b3(); } else { if (Cblue.BMlife - Miceman >= 0) { b4(); } else { if (Cblue.BMlife - Mwolf >= 0) { b5(); } } } } } } void Blue2() { if (Cblue.BMlife - Mdragon >= 0) { b2(); } else { if (Cblue.BMlife - Mninja >= 0) { b3(); } else { if (Cblue.BMlife - Miceman >= 0) { b4(); } else { if (Cblue.BMlife - Mwolf >= 0) { b5(); } else { if (Cblue.BMlife - Mlion >= 0) { b1(); } } } } } } void Blue3() { if (Cblue.BMlife - Mninja >= 0) { b3(); } else { if (Cblue.BMlife - Miceman >= 0) { b4(); } else { if (Cblue.BMlife - Mwolf >= 0) { b5(); } else { if (Cblue.BMlife - Mlion >= 0) { b1(); } else { if (Cblue.BMlife - Mdragon >= 0) { b2(); } } } } } } void Blue4() { if (Cblue.BMlife - Miceman >= 0) { b4(); } else { if (Cblue.BMlife - Mwolf >= 0) { b5(); } else { if (Cblue.BMlife - Mlion >= 0) { b1(); } else { if (Cblue.BMlife - Mdragon >= 0) { b2(); } else { if (Cblue.BMlife - Mninja >= 0) { b3(); } } } } } } void Blue5() { if (Cblue.BMlife - Mwolf >= 0) {b5();} else { if (Cblue.BMlife - Mlion >= 0) {b1();} else { if (Cblue.BMlife - Mdragon >= 0) {b2();} else { if (Cblue.BMlife - Mninja >= 0) {b3();} else {if (Cblue.BMlife - Miceman >= 0){b4();}} } } } }
只是按照题目上的要求在每个函数里加了一些关于武器等的信息。