uvaoj 489

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=430

猜单词游戏。计算机给出一个字符串让你猜,每次猜一个字母。如果字符串中有那个字母,所有该字母都会显示出来。猜错7次就输了,全猜完算赢。到最后也没猜完,算放弃。

这题主要考逻辑能力。每次猜一个字母,就判断一次是已经赢了,还是输了。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int leftt,chance;//left表示还剩多少没有猜对,chance表示还有几次可以猜错
 4 char s[200],s2[200];
 5 int win,lose;
 6 void guess(char ch)
 7 {
 8     int bad=1;
 9     for(int i=0; i<strlen(s); i++)
10     {
11         if(ch==s[i])
12         {
13             leftt--;
14             s[i]=' ';
15             bad=0;
16         }
17     }
18     if(bad)chance--;
19     if(!leftt)win=1;
20     if(!chance)lose=1;
21 }
22 int main()
23 {
24     int rnd;
25     while(~scanf("%d",&rnd),rnd!=-1)
26     {
27         scanf("%s %s",s,s2);
28         printf("Round %d
",rnd);
29         win=lose=0;
30         leftt=strlen(s);
31         chance=7;
32         for(int i=0; i<strlen(s2); i++)
33         {
34             guess(s2[i]);
35             if(win||lose)break;
36         }
37         if(win)printf("You win.
");
38         else if(lose)printf("You lose.
");
39         else
40             printf("You chickened out.
");
41         memset(s,'',sizeof(s));
42         memset(s2,'',sizeof(s2));
43     }
44     return 0;
45 }
原文地址:https://www.cnblogs.com/fqfzs/p/9947710.html