北大MOOC第三周 005:魔兽世界之一:备战

  题目描述 http://cxsjsxmooc.openjudge.cn/2017t3fallw32/005/

#include <iostream>
#include<vector>
#include<string>
#include<map>
#include<iomanip>
using namespace std;
typedef map<string, int> strintmap;

static strintmap strength;
//map<string, int> strength;
//14:30-16:30
class Warcraft {
public:
	vector<string> nameSeq_;
	string name_;
	int lifeNum_;
	//正在构造的第几个
	int ith;
	int countSum;
	int countIth[5];
	//是否已经输出构造完的消息了
	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++;
				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为什么不行???
				
				//bug 5  忘写这一句话了
				//bug 6 一开始把这句话写在ith=(ith+1)%5下面了。。。。
				lifeNum_ -= strength[nameSeq_[ith]];

				ith = (ith + 1) % 5;

				return true;
			}
		}

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


int main()
{
	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;
}

一个小小的程序,写了两个小时,5个bug。真的是要多写才会少bug,即使程序再简单,以目前自己的水平,仍然是写不好。一开始也不用想着刷leetCode了,先把Mooc上的课后习题做做吧!

bug:

1、if else 那里逻辑一开始有问题

2、在if条件判断里使用了i++,这是致命的错误,不应该再犯

3、循环赋值语句a[i]写成了a[0],逗呢,这么多低级错误,反应的还是代码写的少

4、这里为啥不行????ith=(ith++)%5 这样的话,i输出的值是 1 2 3 4 5 ,为什么不是0 1 2 3 4 呢?没搞懂

//这里为什么输出的是 1 2 3 4 5 ????
int i = 0;
while (1)
{
   i = (i++) % 5;
   cout << i << endl;
}

 5、忘写了生命值减少的语句

待解决的问题:

程序中本来map想放在类内,但是类外无法初始化,导致编译不通过,还未解决。

20190317补充:这个题还真的不简单。。。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值