紫书UVA 489

第一篇博客,虽然是个水题,但对我这种小白还是挺艰难的。

思路难点是对已经查过的字符的标记。

此题要注意:猜已经猜过的字母也算错

题目:https://vjudge.net/problem/UVA-489

AC代码:

#include<iostream>
#include<cstring>
using namespace std;
char s[1000],s1[1000];
int lef,chance,win,lose;//lef剩余要猜的数量 chance剩余机会 
void guess(char c)
{
	int flag=1;
	for(int i=0;i<strlen(s);i++)
	{
		if(s[i]==c)
		{
			lef--;
			s[i]=' ';//此处很巧妙 
			flag=0;
		}
	}
	if(flag) 
	--chance;
	if(!chance)
	lose=1;
	if(!lef)
	win=1;
}
int main()
{
	
	int n;
	while(cin>>n&&n!=-1)
	{
		cin>>s>>s1;
		cout<<"Round "<<n<<endl;
		win=lose=0;
		lef=strlen(s);
		chance=7;
		for(int i=0;i<strlen(s1);i++)
		{
			guess(s1[i]);
			if(win||lose) break;
		}
		if(win)cout<<"You win."<<endl;
		else if(lose)cout<<"You lose."<<endl;
		else cout<<"You chickened out."<<endl;
	}
	
	return 0;
} 



posted @ 2018-04-07 00:14  MCQ  阅读(104)  评论(0编辑  收藏  举报