Han gm an Judge 模拟

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: 
   1. The  contestant tries to solve to puzzle by guessing one letter at a time. 
   2. Every  time a guess is correct, all the characters in the word that match  the guess will b e \turned 
      over." For  example,  if your guess is \o" and  the word  is \b o ok", then b oth \o"s in the solution 
      will b e counted as \solved." 
   3. Every  time a  wrong  guess is made,  a stroke will b e added to the  drawing  of a hangman,   which 
      needs 7 strokes to complete.  Each  unique  wrong  guess only counts  against the contestant once. 
Han gm an Judge 模拟_第1张图片
   4. If the drawing of the hangman   is completed  b efore the contestant has successfully guessed all the 
      characters of the word, the  contestant loses. 
   5. If the contestant has  guessed all the characters  of the word  b efore the drawing is complete,  the 
      contestant wins  the game. 
   6. If the contestant do es not guess enough  letters to either win or lose, the contestant chickens out. 
    Your  task as the  \Hangman    Judge"  is to determine, for each game,  whether   the contestant  wins, 
loses, or fails to nish a game. 


Input 

Your  program  will b e given a series of inputs regarding the status of a game. All input will b e in lower 
case. The   rst line of each section will contain a numb er to indicate which  round  of the game  is b eing 
played; the next  line will b e the solution to the puzzle; the last line is a sequence of the guesses made 
by the contestant.  A round  numb  er of -1 would  indicate the end of all games (and  input). 


Output 

The  output  of your program  is to indicate which round  of the game  the contestant  is currently playing 
as well as the result of the game. There  are three p ossible results: 

You  win. 
You  lose. 

You  chickened   out. 


Sample   Input 



cheese 
chese 

cheese 
abcdefg 

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;
}



你可能感兴趣的:(模拟,An,GM,judge,han)