zoukankan      html  css  js  c++  java
  • Hangman Judge UVA

    这道题在《算法竞赛入门经典》(第2版)上有点小问题,不能AC,稍稍改了guess函数部分就OK
    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
    SampSample Output
    Round 1
    You win.
    Round 2
    You chickened out.
    Round 3
    You lose.

    AC过的:

    #include<stdio.h>
    #include<string.h>
    #define maxn 100
    int loc, chance;	//loc是还需要猜几个位置,chance是还有多少次机会
    char s1[maxn], s2[maxn];	//s1代表计算机想的单词,s2代表玩家的猜测
    int win, lose;	//根据win和lose的0和1状态判断输赢
    void guess(char ch) {
    	int bad = 1;	//猜错标志
    	for (int i = 0; i < strlen(s1); i++)	//遍历计算机给的单词
    		if (s1[i] == ch) {//如果猜中了一个字母
    			if (--loc == 0) {//如果都猜中了
    				win = 1;
    				break;
    			}
    			s1[i] = ' ';	//猜过的字母再猜一次就算猜错,所以这里猜过的字母用空格来替换
    			bad = 0;
    		}
    		if (bad)
    			if (--chance == 0)	lose = 1;
    }
    
    int main() {
    	int Round;	//回合数
    	while (scanf("%d%s%s", &Round, s1, s2)==3&&Round!=-1)
    	{
    		printf("Round %d
    ", Round);	//先打印回合数
    		win = lose = 0;		//每一组新的数据之前初始化
    		loc = strlen(s1);
    		chance = 7;		//最多能猜错6次,因此共有7次机会,第7次不能再错
    		for (int i = 0; i < strlen(s2); i++)
    		{
    			guess(s2[i]);		//猜一个字母
    			if (win || lose||chance==0) break;		//判断状态
    		}
    		if (win) printf("You win.
    ");
    		else if(lose&&!chance) printf("You lose.
    ");	//只有chance用完,也即为0时才能算lose
    		else printf("You chickened out.
    ");	//chance次数不为0时,算放弃
    	}
    	return 0;
    }
  • 相关阅读:
    iOS NSUserDefaults 存储可变数组问题
    iOS之[文件下载 / 大文件下载 / 断点下载]
    macOS 新手开发:第 2 部分
    iOS 游戏素材
    iOS 动画
    macOS 开发
    iOS 之访问权限以及跳转到系统界面
    蓝桥杯—ALGO-18 单词接龙(DFS)
    蓝桥杯—ALGO-12 幂方分解(递归递推)
    蓝桥杯—ALGO-131 Beaver's Calculator
  • 原文地址:https://www.cnblogs.com/Lin-Ff2394/p/10325634.html
Copyright © 2011-2022 走看看