Hangman Judge UVA - 489

来源:互联网 发布:网站外链优化的作用 编辑:程序博客网 时间:2024/05/22 03:52

Hangman Judge UVA - 489

题目描述
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 be “turned
over.” For example, if your guess is ‘o’ and the word is “book”, then both ‘o’s in the solution will
be counted as “solved”.
3. 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.


| |
| O
| /|\
| |
| / \
_|
| |__
|___|
4. If the drawing of the hangman is completed before 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 before the drawing is complete, the
contestant wins the game.
6. If the contestant does 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 finish a game.

Input
Your program will be given a series of inputs regarding the status of a game. All input will be in lower
case. The first line of each section will contain a number to indicate which round of the game is being
played; the next line will be the solution to the puzzle; the last line is a sequence of the guesses made
by the contestant. A round number 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 possible results:
You win.
You lose.
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时你失败,结果输出You lose.,当第一串字符全部显示出来时你赢,结果输出You win.,如果你的错误次数没达到7而且猜完后第一串字符没有全部显示出来则输出You chickened out.

思路
简单的模拟

代码如下

#include <iostream> #include <cstdio>#include <cstdlib>#include <fstream>#include <math.h>#include <algorithm>#include <climits>#include <cstring>#include <string>#include <set>#include <queue>#include <stack>#include <vector>#include <list>#include<sstream>#include<ctime>using namespace std;int main(){    int n,l,sum;    char s1[1000],s2[1000];    int f1[30],f2[30];    while(1)    {        cin>>n;        getchar();        if(n==-1)        return 0;        memset(f1,0,sizeof(f1));        memset(f2,0,sizeof(f2));        gets(s1);        gets(s2);        l=strlen(s1);        for(int i=0;i<l;i++)   //f1记录字符串1中有哪些字母出现        {            f1[s1[i]-'a'+1]=1;        }           sum=0;        for(int i=1;i<=26;i++)//sum记录字符串1中出现的不同字母总和        sum+=f1[i];        l=strlen(s2);        int flag1=0,flag2=0,cnt=0;        for(int i=0;i<l;i++)        {            if(f1[s2[i]-'a'+1]==1)// 如果猜测的字母在字符串1中有出现            {                if(f2[s2[i]-'a'+1]==0)// 如果该字母没被猜过                {                   f2[s2[i]-'a'+1]=1; // 将其标记为1表示猜过                    sum--;                              if(!sum)  //如果sum为0表示全被猜完,结束                    {                        flag1=1;                        break;                    }                }            }            else   //如果猜测的字母没在字符串1中出现,说明猜错            {                cnt++;                if(cnt==7) //如果错误次数到达7,结束                {                    flag2=1;                    break;                }            }        }        if(flag1==1) //输出        {            printf("Round %d\n",n);            printf("You win.\n");        }        else if(flag2==1)        {            printf("Round %d\n",n);            printf("You lose.\n");        }        else         {            printf("Round %d\n",n);            printf("You chickened out.\n");        }    }      return 0; } 
0 0