UVa 489 Hangman Judge
2016-06-12 14:52
369 查看
刽子手游戏,计算机提出一个字符串,每一次可以猜一个字母,做多可以猜错7次。
#include<iostream>
#include<string>
using namespace std;
bool find(const char a, const string &s, bool *bArr)
{
bool find = false;
for (string::const_iterator iter = s.begin(); iter != s.end(); ++iter)
if (*iter == a && !bArr[iter - s.begin()]) {
find = true;
bArr[iter - s.begin()] = true;
}
if (find == true)
return true;
else
return false;
}
int main()
{
#ifndef UVa
FILE *fp;
freopen_s(&fp, "data.in.txt", "r", stdin);
freopen_s(&fp, "data.out.txt", "w", stdout);
#endif;
int round;
string sGame;
string sContestant;
int strokeCnt = 0;
while (cin >> round) {
if (round == -1)
break;
strokeCnt = 0;
cin >> sGame >> sContestant;
bool *bArr = new bool[sGame.size()];
for (int i = 0; i < sGame.size(); ++i)
bArr[i] = false;
for (string::iterator iter = sContestant.begin(); iter != sContestant.end(); ++iter) {
bool bFnd = find(char(*iter),sGame,bArr);
if (!bFnd) {
strokeCnt++;
if (strokeCnt == 7)
break;
}
bool bTemp = true;
for (int i = 0; i < sGame.size(); ++i)
if (bArr[i] == false) {
bTemp = false;
break;
}//endif
if (bTemp)
break;
}//endfor
cout << "Round " << round << endl;
bool bTemp = true;
for (int i = 0; i < sGame.size(); ++i)
if (bArr[i] == false) {
bTemp = false;
break;
}
if (strokeCnt == 7)
cout << "You lose." << endl;
else if (bTemp == true)
cout << "You win." << endl;
else
cout << "You chickened out." << endl;
delete[] bArr;
bArr = 0;
}
return 0;
}
#include<iostream>
#include<string>
using namespace std;
bool find(const char a, const string &s, bool *bArr)
{
bool find = false;
for (string::const_iterator iter = s.begin(); iter != s.end(); ++iter)
if (*iter == a && !bArr[iter - s.begin()]) {
find = true;
bArr[iter - s.begin()] = true;
}
if (find == true)
return true;
else
return false;
}
int main()
{
#ifndef UVa
FILE *fp;
freopen_s(&fp, "data.in.txt", "r", stdin);
freopen_s(&fp, "data.out.txt", "w", stdout);
#endif;
int round;
string sGame;
string sContestant;
int strokeCnt = 0;
while (cin >> round) {
if (round == -1)
break;
strokeCnt = 0;
cin >> sGame >> sContestant;
bool *bArr = new bool[sGame.size()];
for (int i = 0; i < sGame.size(); ++i)
bArr[i] = false;
for (string::iterator iter = sContestant.begin(); iter != sContestant.end(); ++iter) {
bool bFnd = find(char(*iter),sGame,bArr);
if (!bFnd) {
strokeCnt++;
if (strokeCnt == 7)
break;
}
bool bTemp = true;
for (int i = 0; i < sGame.size(); ++i)
if (bArr[i] == false) {
bTemp = false;
break;
}//endif
if (bTemp)
break;
}//endfor
cout << "Round " << round << endl;
bool bTemp = true;
for (int i = 0; i < sGame.size(); ++i)
if (bArr[i] == false) {
bTemp = false;
break;
}
if (strokeCnt == 7)
cout << "You lose." << endl;
else if (bTemp == true)
cout << "You win." << endl;
else
cout << "You chickened out." << endl;
delete[] bArr;
bArr = 0;
}
return 0;
}
相关文章推荐
- jvm系列五:JVM调优总结
- HA高可用实验报错
- OpenCV3.1.0读写XML文件
- 【大数据算法】:apriori算法详解,非常清晰
- 基于canvas的图片轮播(h5)
- 设计模式_Java多态,继承,封装
- struts2的拦截器自定义,使用默认拦截器,组合拦截器堆 的案例
- keystone uwsgi failed
- (转)Eclipse New Server 【无法输入server name】
- 让我们一起学习Makefile
- caffe_.mexa64': libcaffe.so: cannot open shared object file: No such file or directory
- SQL查询——示列代码(二)
- tabViewCell重用
- 【学习笔记】Java SE环境安装和配置
- 原码, 反码和补码
- 欢迎使用CSDN-markdown编辑器
- Android中webview js与native方法互相调用
- PRO2-Day7
- Swap Nodes in Pairs
- 通过 http 协议上传文件