逐个字符地处理,全部猜中及“Game Over”时及时退出。
以下是我的代码:
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int kMax(107);
int main()
{
/*
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
//*/
int n;
while(cin>>n && n!=-1)
{
cout<<"Round "<<n<<endl;
string ans,guess;
cin>>ans>>guess;
bool hash_ans[kMax],hash_guess[kMax],hash_guessed[kMax];
fill_n(hash_ans,kMax,false);
fill_n(hash_guess,kMax,false);
fill_n(hash_guessed,kMax,false);
for(int i=0;i<ans.size();i++)
hash_ans[ans[i]-'A']=true;
int wrong(0);
for(int i=0;i<guess.size();i++)
{
if(hash_guessed[guess[i]-'A'])
continue;
hash_guessed[guess[i]-'A']=true;
if(hash_ans[guess[i]-'A'])
{
hash_guess[guess[i]-'A']=true;
//
if(memcmp(hash_ans,hash_guess,kMax*sizeof(bool))==0)
break;
// [ERROR]
}
else
{
wrong++;
if(wrong>=7)
break;
}
}
if(wrong>=7)
cout<<"You lose."<<endl;
else if(memcmp(hash_ans,hash_guess,kMax*sizeof(bool))==0)
cout<<"You win."<<endl;
else cout<<"You chickened out."<<endl;
}
return 0;
}
posted on 2011-04-07 12:48
lee1r 阅读(614)
评论(0) 编辑 收藏 引用 所属分类:
题目分类:字符串处理