题目很简单,猜单词游戏,只能错6次,超过6次算输了,猜错的次数没到7,且结束了猜,则为放弃,目标单词中的字母全猜了且次数小于等于6算赢。
需要注意的是,猜的字符串中,重复出现的字符,算一次,举例:目标单词:chess。猜的字符串为dddeswqqax,这里算放弃,因为d只算一次。
下面的是AC的代码:
#include <iostream>
#include <cstring>
using namespace std;
char res[110], ans[110];
bool win, lose;
bool r[110];
bool tag[30];
int len1, len2;
bool guess(char c)
{
if(tag[c - 'a'])
return true;
bool flag = false;
for(int i = 0; i < len1; i++)
{
if(res[i] == c)
{
flag = true;
r[i] = true;
}
}
tag[c - 'a'] = true;
return flag;
}
bool cheak()
{
bool flag = true;
for(int j = 0; j < len1; j++)
if(!r[j])
flag = false;
return flag;
}
int main()
{
// freopen("489.txt", "r", stdin);
int n;
while(cin >> n)
{
if(n == -1)
break;
cin >> res >> ans;
win = lose = false;
memset(r, false, sizeof(r));
memset(tag, false, sizeof(tag));
len1 = strlen(res);
len2 = strlen(ans);
int count = 0;
for(int i = 0; i < len2; i++)
{
if(!guess(ans[i]))
count++;
if(count <= 6 && cheak())
{
win = true;
break;
}
else if(count >= 7 && !cheak())
{
lose = true;
break;
}
}
cout << "Round " << n << endl;
if(!win && !lose)
cout << "You chickened out." << endl;
else if(win && !lose)
cout << "You win." << endl;
else if(!win && lose)
cout << "You lose." << endl;
}
return 0;
}