算法之路二:刘汝佳算法竞赛入门经典 4.11刽子手游戏 UVa 489

来源:互联网 发布:千牛与淘宝什么区别 编辑:程序博客网 时间:2024/05/18 03:57
#include<stdio.h>#include<string.h>#define maxn 100int left,chance;//left剩余位置,chance机会次数 char s[maxn],s2[maxn];//s答案,s2猜测 int win,lose;void guess(char ch);int main(){    int rnd;    while(scanf("%d%s%s",&rnd,s,s2)==3&&rnd!=-1)//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]);//根据s2猜测             if(win||lose) break;//注意win和lose位置         }        if(win) printf("You win.\n");        else if(lose) printf("You lose.\n");        else printf("You chickened out.\n");    }    return 0;}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) --chance;//猜错chance-1         if(!chance) lose=1;//没机会舒         if(!left) win=1;//全部猜中赢     }}
1 0
原创粉丝点击