https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=show_problem&problem=430
题目:猜单词游戏,规则如下:
1. 竞猜者每次猜一个字母;
2. 每次竞猜正确,单词中所有此字符均显示已猜中;
3. 竞猜者猜错7次,则竞猜者输;同一字符只算一次错误;
4. 竞猜者在猜出全部的字符,则竞猜者赢。如果还没有输赢就不竞猜了,则竞猜者放弃;
Sample Input
1
cheese
chese
2
cheese
abcdefg
3
cheese
abcdefgij
-1
Sample Output
Round 1
You win.
Round 2
You chickened out.
Round 3
You lose.
思路:因为所有输入均为小写字母,所以用数组aa记录竞猜单词中出现的字符。然后遍历竞猜者的序列,如果aa[s[i]-'a']=1,则猜中,否则为没有猜中,记录没有猜中的次数count,如果count>=7 则竞猜者输;
如果count<7 且 存在aa[i]!=0,则竞猜者放弃比赛。如果count<7 且 aa数组值均为0,则竞猜者赢。
1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 using namespace std;
5
6 int main()
7 {
8 // freopen("input.txt","r",stdin);
9 int t,count,i,j;
10 string s1,s2;
11 int aa[27];
12 bool flag,f;
13 while(cin>>t&&t!=-1)
14 {
15 f=false;
16 flag=false;
17 count=0;
18 memset(aa,0,sizeof(aa));
19 cin>>s1>>s2;
20 for(i=0;i<s1.length();i++)
21 aa[s1[i]-'a']=1;
22 for(j=0;j<s2.length();j++)
23 {
24 if(aa[s2[j]-'a'])
25 aa[s2[j]-'a']=0;
26 else
27 count++;
28 if(count>=7)
29 break;
30 for(i=0;i<27;i++)
31 if(aa[i])
32 {
33 flag=true;
34 break;
35 }
36 if(flag)
37 {
38 flag=false;
39 continue;
40 }
41 f=true;
42 break;
43 }
44 cout<<"Round "<<t<<endl;
45 if(count>=7)
46 cout<<"You lose."<<endl;
47 else if(f)
48 cout<<"You win."<<endl;
49 else
50 cout<<"You chickened out."<<endl;
51 }
52 return 0;
转载于:https://www.cnblogs.com/aiheshan/p/5735026.html