A CM Conte st Problems Archive Univers ity of Valladolid (SPAIN)
489 Hangman Judge
In "Hangman Judge," you are to write a program that judges a series of Hangman games. For each
game, the answer to the puzzle is given as well as the guesses. Rules are the same as the classic game of
hangman, and are given as follows:You 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.
题目大意:一个类似于猜单词的游戏,如果你猜对一个答案中包含的字母,那么所有字母都会显示,如果猜的是答案中不包含的字母或者已经猜过的字母,对方得一分。如果对方先得到7分,你就输了;如果你先猜出所有的字母,你就赢了;最后不赢不输就是放弃了。
思路:首先统计答案中每一个字母是否出现,然后再计算出答案中所有不相同的字母总数,接着遍历猜测的顺序,如果字母不存在或者已经猜过,对方加一分,否则自己加一分。如果对方先达到7分,就输了,如果自己先猜出所有字母,就赢了,否则就放弃。
#include <iostream> #include <stdio.h> #include <string.h> #define MAX 100000 using namespace std; int main() { int round,res_c[26],len,i; int sov,sto,uni; int win; char res[MAX],guess[MAX]; while(~scanf("%d",&round)&&round!=-1) { scanf("%s%s",res,guess); memset(res_c,0,sizeof(res_c)); win=uni=sov=sto=0; len=strlen(res); for(i=0; i<len; i++) res_c[res[i]-'a']++; for(i=0;i<26;i++) if(res_c[i]!=0) uni++; len=strlen(guess); for(i=0; i<len; i++) { if(res_c[guess[i]-'a']==0||res_c[guess[i]-'a']==-1) { sto++; res_c[guess[i]-'a']=-1; } else { res_c[guess[i]-'a']=-1; sov++; } if(sto==7) { printf("Round %d\n",round); printf("You lose.\n"); win=1; break; } else if(sov==uni) { printf("Round %d\n",round); printf("You win.\n"); win=1; break; } } if(win==0) { printf("Round %d\n",round); printf("You chickened out.\n"); } } return 0; }