程序设计与算法(三)测验题

魔兽世界之一:备战

#include<iostream>
using namespace std;
#pragma warning(disable:4996)

class CHeadQuarter;
class CWarrior {
private:
	int No;
	int KindNo;
	CHeadQuarter* p;
public:
	static const char* WarriorName[5];
	static int strength[5];
	CWarrior(int No_, int KindNo_, int strength_, CHeadQuarter* p_) :No(No_), KindNo(KindNo_),p(p_) {}
	void PrintResult(int t);

};
class CHeadQuarter {
	int color;
	int strength;
	int anWarriorNum[5];
	int TotalNum = 0;
	int CurMakIdx = 0;
	CWarrior* p[10000];
	bool Stopped = false;
public:
	static int MakSeq[2][5];
	friend class CWarrior;
	CHeadQuarter(int color_, int strength_) :color(color_), strength(strength_){
		for (auto& e : anWarriorNum)
			e = 0;
	}
	~CHeadQuarter() {
		for (int i = 0;i < TotalNum;i++)
			delete p[i];
	}
	bool creat(int t);
	const char* GetColor() {
		return color ? "blue" : "red";
	}
};
void CWarrior::PrintResult(int t) {
	printf("%03d %s %s %d born with strength %d,%d %s in %s headquarter\n",
		t, p->GetColor(), CWarrior::WarriorName[KindNo], No, CWarrior::strength[KindNo], p->anWarriorNum[KindNo], WarriorName[KindNo], p->GetColor());
}
bool CHeadQuarter::creat(int t) {
	if (Stopped)
		return false;
	int timer = 0;
	while (strength < CWarrior::strength[MakSeq[color][CurMakIdx]] && timer < 5) {
		++timer;
		CurMakIdx = (CurMakIdx + 1) % 5;
	}
	if (timer == 5) {
		Stopped = true;
		printf("%03d %s headquarter stops making warriors\n", t, GetColor());
		return false;
	}
	int KindNo = MakSeq[color][CurMakIdx];
	p[TotalNum] = new CWarrior(TotalNum+1, KindNo, CWarrior::strength[KindNo], this);
	strength -= CWarrior::strength[KindNo];
	anWarriorNum[KindNo]++;
	p[TotalNum++]->PrintResult(t);
	CurMakIdx = (CurMakIdx + 1) % 5;
	return true;
}

const char* CWarrior::WarriorName[5] = { "dragon", "ninja", "iceman", "lion", "wolf" };
int CWarrior:: strength[5];
int CHeadQuarter::MakSeq[2][5] = { {2,3,4,1,0},{3,0,1,2,4} };
int main() {
	//freopen("C:\\Users\\czh\\Desktop\\2.txt", "r", stdin);
	int CaseNum;
	scanf("%d", &CaseNum);
	for (int i = 1;i <= CaseNum;++i) {
		printf("Case:%d\n", i);
		int m;
		scanf("%d", &m);
		for (int j = 0;j < 5;j++)
			scanf("%d", &CWarrior::strength[j]);
		CHeadQuarter r(0, m), b(1, m);
		int t = 0;
		while (1) {
			bool r_test, b_test;
			r_test = r.creat(t);
			b_test = b.creat(t);
			++t;
			if (r_test==0 && b_test==0)
				break;
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值