程序设计与算法(三)C++:第五章poj代码(续)

课程:北京大学程序设计与算法(三)     MOOC

OJ: OpenJudge

021:魔兽世界之二:装备

这个题是在一的基础上进行的,没看过的可以参考一下

魔兽世界一

好了,那么这篇文章我们就来分析一下新增了什么吧。

首先加了武器,我们可以用常量数组来存储,

每种武士还有其它技能,我们可以用一个成员函数来实现,还是比较容易的。

武器数组

string wq[3] = { "sword","bomb","arrow" };

对应下标跟其编号对应

putwq函数

void putwq(int k)
	{
		if (ws[k] == "dragon")
		{
			float shiqi =1.0*live / wslive[k];
			cout << "It has a " << wq[sum % 3] << ",and it's morale is " << fixed << setprecision(2) << shiqi<<endl;
		}
		else if (ws[k] == "ninja")
		{
			cout << "It has a " << wq[sum % 3] << " and a " << wq[(sum + 1) % 3] << endl;
		}
		else if (ws[k] == "iceman")
		{
			cout << "It has a " << wq[sum % 3] << endl;
		}
		else if (ws[k] == "lion")
		{
			cout << "It's loyalty is " << live << endl;
		}
	}

这个其实就是条件判断,比较简单,初学者可以学习一下C++的格式化输出,用fixed和setprecision来控制小数点后输出位数,默认四舍五入。

born()更新

void born()
	{
		for (int i = tag; i < 5; i++)
			if (wslive[i] <= live)
			{
				live -= wslive[i];
				cout << setw(3) << setfill('0') << tim << ' ' << name << ' ' << ws[i] << ' ' <<
					++sum << " born with strength " << wslive[i] << ','
					<< ++num[i] << ' ' << ws[i] << " in " << name << " headquarter" << endl;
				putwq(i);
				tag = (i + 1) % 5;
				return;
			}
		tag = 0;
		for (int i = tag; i < 5; i++)
			if (wslive[i] <= live)
			{
				live -= wslive[i];
				cout << setw(3) << setfill('0') << tim << ' ' << name << ' ' << ws[i] << ' ' <<
					++sum << " born with strength " << wslive[i] << ','
					<< ++num[i] << ' ' << ws[i] << " in " << name << " headquarter" << endl;
				putwq(i);
				tag = (i + 1) % 5;
				return;
			}

其实,就是调用了putwq函数,没什么特殊的。

其它代码跟一一样。

完整代码如下:

#include<iostream>
#include<iomanip>
#include<string.h>
#include<cmath>
using namespace std;


int N, m, tim; int al[5]; int mmin = 10005;
string biao[5] = { "dragon","ninja","iceman","lion","wolf" };
string rws[5] = { "iceman","lion","wolf","ninja","dragon" };
string bws[5] = { "lion","dragon","ninja","iceman","wolf" };
string wq[3] = { "sword","bomb","arrow" };
int rwsl[5];   //红色武士生命值
int bwsl[5];   //蓝色武士生命值


class slb {
private:
	string name;
	int live;
	string ws[5]; //武士顺序名称
	int wslive[5];   //对应生命值
	int num[5] = { 0 };
	int sum = 0;
	int tag = 0;
public:
	int flag = 1;
	//int state = 1;
	slb(string nname, int llive) {
		name = nname;
		live = llive;
		if (name == "red")
		{
			for (int i = 0; i < 5; i++)
			{
				ws[i] = rws[i];
				wslive[i] = rwsl[i];
			}
		}
		else
		{
			for (int i = 0; i < 5; i++)
			{
				ws[i] = bws[i];
				wslive[i] = bwsl[i];
			}
		}
	}
	void putwq(int k)
	{
		if (ws[k] == "dragon")
		{
			float shiqi =1.0*live / wslive[k];
			cout << "It has a " << wq[sum % 3] << ",and it's morale is " << fixed << setprecision(2) << shiqi<<endl;
		}
		else if (ws[k] == "ninja")
		{
			cout << "It has a " << wq[sum % 3] << " and a " << wq[(sum + 1) % 3] << endl;
		}
		else if (ws[k] == "iceman")
		{
			cout << "It has a " << wq[sum % 3] << endl;
		}
		else if (ws[k] == "lion")
		{
			cout << "It's loyalty is " << live << endl;
		}
	}
	void born()
	{
		for (int i = tag; i < 5; i++)
			if (wslive[i] <= live)
			{
				live -= wslive[i];
				cout << setw(3) << setfill('0') << tim << ' ' << name << ' ' << ws[i] << ' ' <<
					++sum << " born with strength " << wslive[i] << ','
					<< ++num[i] << ' ' << ws[i] << " in " << name << " headquarter" << endl;
				putwq(i);
				tag = (i + 1) % 5;
				return;
			}
		tag = 0;
		for (int i = tag; i < 5; i++)
			if (wslive[i] <= live)
			{
				live -= wslive[i];
				cout << setw(3) << setfill('0') << tim << ' ' << name << ' ' << ws[i] << ' ' <<
					++sum << " born with strength " << wslive[i] << ','
					<< ++num[i] << ' ' << ws[i] << " in " << name << " headquarter" << endl;
				putwq(i);
				tag = (i + 1) % 5;
				return;
			}

	}
	void destroy()
	{
		if (flag)
			cout << setw(3) << setfill('0') << tim << ' ' << name
			<< " headquarter stops making warriors" << endl;

		flag = 0;

	}
	bool ifcontinue()
	{
		if (live < mmin)
			return 0;

		return 1;
	}
};
void init()
{
	tim = 0;
	mmin = 100000;
	for (int i = 0; i < 5; i++)
		for (int j = 0; j < 5; j++)
		{
			if (biao[i] == rws[j])
				rwsl[j] = al[i];
			if (biao[i] == bws[j])
				bwsl[j] = al[i];
		}
	for (int i = 0; i < 5; i++)
		mmin = min(al[i], mmin);   //存储最小的生命值
}
int main()
{

	cin >> N;
	for (int i = 1; i <= N; i++)
	{
		cin >> m; //生命值上限
		for (int j = 0; j < 5; j++)
			cin >> al[j];   //每种武士生命值
		init();
		slb* red = new slb("red", m), * blue = new slb("blue", m);


		cout << "Case:" << i << endl;
		do
		{
			if (red->ifcontinue())
				red->born();
			else
				red->destroy();

			if (blue->ifcontinue())
				blue->born();
			else blue->destroy();

			tim++;
		} while (red->ifcontinue() || blue->ifcontinue());


		red->destroy();
		blue->destroy();

		delete red, blue;

	}


	return 0;
}

  • 32
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值