UVa 489 Hangman Judge

来源:互联网 发布:淘宝卖家体验用心在哪 编辑:程序博客网 时间:2024/06/06 19:38

本题还是利用字符串匹配,根据题目意思如果匹配的字符错误次数超过7次,就会输。所以本题可以利用变量计算统计共出现错误的次数即可。

#include <stdio.h>#include <string.h>#define maxn 100int left, chance;char s[maxn], s2[maxn];int win, lose;//flag数组void guess(char ch){    int bad = 1;    for(int i = 0; i < strlen(s); i++)        if(s[i] == ch)//匹配成功将该位置的字符置为空        {            left--;            s[i] = ' ';            bad = 0;        }        if(bad)//未匹配到bad为1            --chance;        if(!chance)//当chance变量为0时            lose = 1;        if(!left)//当left变量为0时            win = 1;}int main(){    int rnd;    while(scanf("%d%s%s", &rnd, s, s2) == 3 && rnd != -1)    {        printf("Round %d\n", rnd);        win = lose = 0;        left = strlen(s);        chance = 7;        for(int i = 0; i < strlen(s2); i++)        {            guess(s2[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;}
1 0
原创粉丝点击