UVA489 Hangman Judge

来源:互联网 发布:服务器入门书籍 知乎 编辑:程序博客网 时间:2024/05/11 22:53

问题链接:UVA489 Hangman Judge。

问题简述:(略),参见原题链接。

问题分析:这是一个模拟题。

程序说明:程序中定义了若干宏定义,使得程序可阅读性增强。函数guess()中的逻辑做了适当的改进,更加合理快速。


AC的C语言程序如下:

/* UVA489 Hangman Judge */#include <stdio.h>#include <string.h>#define TRUE  1#define FALSE 0#define MAXN 128char s[MAXN], t[MAXN];int win, lose, chance, left, lens, lent;void guess(char c){    int bad, i;    bad = TRUE;    for(i=0; i<lens; i++)        if(s[i] == c) {            if(--left == 0) {                win = 1;                return;            }            s[i] = ' ';            bad = FALSE;        }    if(bad)        if(--chance == 0)            lose = TRUE;}int main(void){    int round, i;    while(scanf("%d%s%s", &round, s, t) != EOF && round != -1) {        printf("Round %d\n", round);        left = lens = strlen(s);        lent = strlen(t);        win = lose = FALSE;        chance = 7;        for(i=0; i<lent; i++) {            guess(t[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;}


参考链接:Hangman Judge。


1 0