UVA489解题报告(刽子手游戏)

来源:互联网 发布:如何管理网络客服 编辑:程序博客网 时间:2024/05/22 05:00

题目抽象

  给你一个单词,你要猜出这个单词所含的所有字母。如果你猜错了7次输出“You lose”,如果你全猜出来了输出“You win”,如果你猜了一半就不猜了输出“You chickened out”。但是我们不是真的猜。实际上,我们需要猜的单词是计算机给出的,我们猜的单词也是计算机给出的。也就是计算机给我们两个单词,我们需要验证是第二个单词是猜对了、猜错了还是放弃了并输出相应的信息。注意:具体的输入输出格式参见uva https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=430

对了,所有单词均由小写字母组成,我们后面的解题利用到了这一特点。

解题思路:利用都是小写字母这一特点解题,首先记录出现了多少不同的字母(total)并开一个数组记录该字母是否出现,进行比较时每次找到出现的字母total-1并且对应的字母由有变为无,如果total变为00则win,如果出错次数达到7次则lose

给出一组我做错的数据

 1
a
asdfghjkl
Round 1
You win.

开始我的是lose,所以wrong answer

AC代码 Time 0.040s

#include<cstdio>#include<cstring>using namespace std;const int maxn=1000+10;int alp[30],count,win,lose,total;char s1[maxn],s2[maxn];void judge(char c){    if(alp[c-'a'])    {        alp[c-'a']=0;        total--;        if(!total) win=1;    }    else    {        count++;        if(count==7) lose=1;    }}int main(){    int T;    while(scanf("%d",&T)==1 && T!=-1)    {        scanf("%s",s1);        win=lose=total=count=0;        memset(alp,0,sizeof(alp));        for(int i=0;i<strlen(s1);i++)        {            if(!alp[s1[i]-'a'])            {                total++;                alp[s1[i]-'a']=1;            }        }        scanf("%s",s2);        for(int i=0;i<strlen(s2);i++)        {            if(win || lose) break;            judge(s2[i]);        }        printf("Round %d\n",T);        if(lose) printf("You lose.\n");        else        {            if(win) printf("You win.\n");            else printf("You chickened out.\n");        }    }    return 0;}