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