首页 » 技术分享 » 北大MOOC第五周003:魔兽世界之二:装备

北大MOOC第五周003:魔兽世界之二:装备

 

一个一个结果对比了,发现结果都对啊,但是没有通过。。。 

其实很简单,就是在原来魔兽世界一的基础上多加了一个武器的功能,然而自己还是搞了一个小时,两周前写的程序都忘完了,发现自己当时写的程序注释不够详细,所以才会导致程序中的变量好多都忘了。吸取教训,要注释的详细一点,你的遗忘速度真的很快!!!

原题地址http://cxsjsxmooc.openjudge.cn/2019t3sprintw5/003/

#include <iostream>
#include<vector>
#include<string>
#include<map>
#include<iomanip>
#include<fstream>
using namespace std;
typedef map<string, int> strintmap;
//每个战士的生命值
static strintmap strength;
//武器名
static vector<string> weapon;
//map<string, int> strength;
//14:30-16:30
//21:25
class Warcraft {
public:
	//司令部名
	string name_;
	//战士名
	vector<string> nameSeq_;

	//生命值
	int lifeNum_;
	//正在构造的第几个
	
	//第几个战士索引
	int ith;
	//第几个战士的数量索引数组
	int countIth[5];
	//总战士人数
	int countSum;

	//是否已经输出构造完的消息了,构造结束标志
	bool hasCout;


	Warcraft(string name, const vector<string> &nameSeq, int lifeNum) {
		name_ = name;
		nameSeq_ = nameSeq;
		lifeNum_ = lifeNum;
		ith = 0;
		countSum = 0;
		hasCout = false;
		for (int i = 0; i < 5; i++)
			countIth[i] = 0;
	}

	bool born(int time)
	{
		for (int i = 0; i < 5; i++)
		{
			//bug1  差点写出bug。。。卧槽
			if (hasCout)
			{
				return false;
			}
			//bug2 不能在这里用i++,因为这里+过后就会影响后面的i
			else if (strength[nameSeq_[ith]] > lifeNum_)
			{
				ith = (ith + 1) % 5;
				continue;
			}
			else
			{
				countIth[ith]++;
				countSum++;
				//bug 5  忘写这一句话了
				//bug 6 一开始把这句话写在ith=(ith+1)%5下面了。。。。
				lifeNum_ -= strength[nameSeq_[ith]];

				cout << setw(3) << setfill('0') << time << " " << name_ << " " << nameSeq_[ith] << " " << countSum << " born with strength " << strength[nameSeq_[ith]] << "," << countIth[ith] << " " << nameSeq_[ith] << " in " << name_ << " headquarter" << endl;
				//bug 4  ith=(ith++)%5为什么不行???

				if (nameSeq_[ith] == "dragon")
				{
					cout << "it has a " <<weapon[countSum%3]<< ", and it's morale is " << fixed <<setprecision(2) << ((float)lifeNum_)/strength[nameSeq_[ith]] << endl;
				}
				else if (nameSeq_[ith] == "ninja")
				{
					cout << "It has a "<<weapon[countSum%3]<<" and a "<<weapon[(countSum+1)%3]<< endl;
				}
				else if (nameSeq_[ith] == "iceman")
				{
					cout << "It has a "<< weapon[countSum % 3] << endl;
				}
				else if (nameSeq_[ith] == "lion")
				{
					cout << "It's loyalty is "<< lifeNum_ << endl;
				}
				ith = (ith + 1) % 5;

				return true;
			}
		}

		cout << setw(3) << setfill('0') << time << " " << name_ << " headquarter stops making warriors" << endl;
		hasCout = true;
		return false;
	}
};


int main()
{
	//静态变量初始化
	weapon.push_back("sword");
	weapon.push_back("bomb");
	weapon.push_back("arrow");

	vector<string> redName;
	vector<string> blueName;
	redName.push_back("iceman");
	redName.push_back("lion");
	redName.push_back("wolf");
	redName.push_back("ninja");
	redName.push_back("dragon");

	blueName.push_back("lion");
	blueName.push_back("dragon");
	blueName.push_back("ninja");
	blueName.push_back("iceman");
	blueName.push_back("wolf");

	char a[5][10] = { "dragon", "ninja", "iceman", "lion", "wolf" };
	int n, M;

	cin >> n;
	for (int ii = 0; ii < n; ii++)
	{
		cin >> M;
		cout << "Case:" << ii + 1 << endl;
		for (int i = 0; i < 5; i++)
		{
			int temp;
			cin >> temp;
			//bug 3 a[i]写成了a[0]。。。
			//strength[a[i]] = temp;
			strength[a[i]] = temp;
		}

		Warcraft wRed("red", redName, M);
		Warcraft wBlue("blue", blueName, M);

		int time = 0;
		//bug 7 这个是在上面while退出时,这一时刻输出了headquarter stops making warriors,但是time++没有执行,所以需要在while之外加上这句
		//bug 8 如果两者是同时退出的话,上面的&&只要第一个wRed不满足后,就不会再执行后面的wBlue了,所以此时不能time++
		//一定要确定谁先执行完,如果使用下面这种同时判断wRed和wBlue,则不能确定是谁先执行完了
		//while (wRed.born(time) && wBlue.born(time))
		//{
		//	time++;
		//}
		while (wRed.born(time))
		{
			wBlue.born(time);
			time++;
		}
		while (wBlue.born(time))
		{
			time++;
		}



		/下面这种情况会遇到的问题是:
		//当输入为
		//5000
		//200 400 6 70 20 时,
		//wRed和wBlue同时到达了最终要输出stops making warriors。但是程序在判断了wRed.born(time)为false后就不会执行后面的了。
		//此时后面的语句不应该time++。  这样就和bug 7那里冲突了,不知道什么时候该time++,因为不知道两个谁先结束,所以最后的解决办法是上面的while (wRed.born(time))  {  }
		//while (wRed.born(time) && wBlue.born(time))
		//{
		//	time++;
		//}

		bug 7 这个是在上面while退出时,这一时刻输出了headquarter stops making warriors,但是time++没有执行,所以需要在while之外加上这句
		//time++;
		//while (wBlue.born(time))
		//{
		//	time++;
		//}
		//while (wRed.born(time))
		//{
		//	time++;
		//}
		//return 0;
	}

	//n = 1, M = 20;
	//for (int i = 0; i < 5; i++)
	//{
	//	strength[a[i]] = i+3;
	//}




	return 0;
}

 

转载自原文链接, 如需删除请联系管理员。

原文链接:北大MOOC第五周003:魔兽世界之二:装备,转载请注明来源!

0