Page 79
题意
输入答案单词和玩家的猜测单词,每次猜一个字母,如果答案单词里有这个字母,则猜对答案单词中所有该字母;如果没有这个字母,则猜错一次。猜错达到7次玩家输,答案单词的所有字母全部猜到则玩家赢,其他情况视为弃权(chickened out)。
Sample Input
1
cheese
chese
2
cheese
abcdefg
3
cheese
abcdefgij
-1
Sample Output
Round 1
You win.
Round 2
You chickened out.
Round 3
You lose.
具体分析见算法竞赛入门经典(第二版)
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cctype>
#include<cmath>
using namespace std;
#define M 130
int lft,chance,win,lose;
char s[M],s1[M];
void gus(char c){
int bad=1;
for(int i=0;i<strlen(s);i++)
if(s[i]==c) {lft--;s[i]=' ';bad=0;}
if(bad) --chance;
if(!chance) lose=1;
if(!lft) win=1;
}
int main()
{
//freopen("in.txt","r",stdin);
int n;
while(scanf("%d%s%s",&n,s,s1)!=EOF&&n!=-1){
printf("Round %d\n",n);
win=lose=0;
lft=strlen(s);
chance=7;
for(int i=0;i<strlen(s1);i++){
gus(s1[i]);
if(win||lose) break;
}
if(win) printf("You win.\n");
else if(lose) printf("You lose.\n");
else printf("You chickened out.\n");
}
return 0;
}