UvaOJ 489

 

     Hangman游戏就是一个猜字游戏,题目读起来很考验英文水平,其最重要的一句话是:

     Every time a wrong guess is made, a stroke will be added to the drawing of a hangman, which needs 7 strokes to complete. Each unique wrong guess only counts against the contestant once.

 

     翻译过来就是:重复输入一个错误答案,只记一次错误。比如猜“book”,你输入‘a’,会增加一次错误次数。如果你再输入一次‘a’,就不再增加错误次数了。

 

     我的思路是:

     首先把输入的字符串化简,去除重复的字母。对谜底和输入的答案都做这样的处理;然后再通过strchr()函数进行判断。这样运行时间还算可以,不知道更快的方法是什么。

 

 

#include <stdio.h>
#include <string.h>
#include <time.h>

int simple(char *a, char *b)
{
	int sig, i, j, n = 1;
	for(i=0;i<strlen(a);i++)
	{
		sig = 0;
		for(j=0;j<n;j++)
			if(a[i] == b[j])
			{
				sig = 1;
				break;
			}
		if(sig!=1)
		{
			b[n-1] = a[i];
			n++;
		}
	}
	return n-1;
}

int main()
{
	char solution[20], sins[20], ans[20], sina[20];
	int round;
	while(scanf("%d", &round) && round != -1)
	{	
		int t = 0, f = 0, i = 0, n, m;
		scanf("%s", solution);
		scanf("%s", ans);
		memset(sina,0,sizeof(sina));
		memset(sins,0,sizeof(sins));
		n = simple(solution,sins);
		m = simple(ans,sina); 
		for(i=0;i<m;i++)
		{
			if(strchr(sins,sina[i]))
				t++;
			else
				f++;
			if(f == 7 || t == n)
				break;	
		}
		printf("Round %d\n",round);
		if(f == 7)
			printf("You lose.\n");
		else if(t == n)
			printf("You win.\n");
		else
			printf("You chickened out.\n");
	}
	printf("%.3lf\n", (double)clock()/CLOCKS_PER_SEC);
	return 0;
}
 

 

你可能感兴趣的:(uva)