UVA - 489 Hangman Judge

/*
  入门经典P80-81有题解,十分详细
  入门经典上有提醒,猜一个已经猜过的字母也算错...我英语理解能力不是太好,猜测应该是原题里的这句话"Each unique wrong guess only counts against the contestant once.",也许是表示每次错误的猜测都是单独起作用的?所以如果两次猜错都是同一个字母,那就错两次?
  
  总而言之,英文阅读能力还是要加强,不然连题目都读不懂 T^T
  
  另外发现,我用C++写的时候,DevC上居然报了一堆编译错,说left变量有问题,后来想了想,left应该算C++的关键字,因为IO流控制输入输出时,是有ios::left表示输入时左对齐的,所以就换成了rest
  
  务必谨慎!~就像之前遇到过的,round和clock也是C++有的函数,不要再用它们定义变量名了...越学越发现直接基础并不太扎实,加油加油,日进有功!
*/


#include <iostream>
#include <cstring>
//#define debug
using namespace std;
const int maxn = 1005;

int rest, chance, win, lose;
char word[maxn], ans[maxn];

void guess(char ch)
{
	bool flag = false;
	int len = strlen(word);
	for (int i = 0; i < len; i++)
	if (word[i] == ch)
	{
		rest--; word[i] = ' '; flag = true;
	}
	if (!flag) chance--;
	if (!chance) lose = 1;
	if (!rest) win = 1;
}

int main()
{
	#ifdef debug
	freopen("E:\\in.txt", "r", stdin);
	freopen("E:\\out.txt", "w", stdout);
	#endif	
	int rnd;
	while (cin >> rnd >> word >> ans && rnd != -1)
	{
		win = lose = 0;
		chance = 7;
		rest = strlen(word);
		int len = strlen(ans);
		
		for (int i = 0; i < len; i++)
		{
			guess(ans[i]); //猜一个字母 
			if (win || lose) break; //检查状态 
		}
		
		//根据结果进行输出 
		cout << "Round " << rnd << endl;
		if (win) cout << "You win."; 
		else if (lose) cout << "You lose.";
		else cout << "You chickened out.";
		cout << endl;
	}	
	#ifdef debug
	fclose(stdin);
	fclose(stdout);
	#endif
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值