例题4-2 刽子手游戏(Hangman Judge,UVa 489)

原题链接:https://vjudge.net/problem/UVA-489
分类:函数
备注:水题
前言:只要好好理解了题目意思就OK了,深感做题理解题意的重要性,好好学英语啊!

代码如下:

#include
#include
void solve(char* s1, char* s2)
{
	int left = 7, len1 = strlen(s1), len2 = strlen(s2);
	for (int i = 0; i < len2; i++)
	{
		int get = 0, win = 1;
		for (int j = 0; j < len1; j++)
		{
			if (s1[j] == s2[i]) { get = 1; s1[j] = '-'; }//猜对的字符变成'-'
			if (s1[j] != '-')win = 0;
		}
		if (win) { printf("You win.\n"); return; }
		if (!get)left--;
		if (!left) { printf("You lose.\n"); return; }
	}
	printf("You chickened out.\n");
}
int main(void)
{
	int kase;
	char s1[10000], s2[10000];
	while (~scanf("%d", &kase) && kase != -1)
	{
		scanf("%s%s", s1, s2);
		printf("Round %d\n", kase);
		solve(s1, s2);
	}
	return 0;
}

你可能感兴趣的:(《算法竞赛入门经典(第2版)》)