| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 6552 | Accepted: 2275 |
Description
After playing several rounds, Ollie has become suspicious that Stan cheats; that is, that he changes the number between Ollie's guesses. To prepare his case against Stan, Ollie has recorded a transcript of several games. You are to determine whether or not each transcript proves that Stan is cheating.
Input
Output
Sample Input
10
too high
3
too low
4
too high
2
right on
5
too low
7
too high
6
right on
0
Sample Output
Stan is dishonest
Stan may be honest
#include <string>
using namespace std;
static const string dishonest="Stan is dishonest";
static const string honst="Stan may be honest";
static const string TooLow="too low";
static const string TooHigh="too high";
static const string Bingo="right on";
int main()
{
string currentResult;
int Highest=11;
int Lowest=0;
int currentAnswer=0;
do
{
cin>>currentAnswer;
cin.get();
if (currentAnswer!=0)
{
getline(cin,currentResult);
if (currentResult.compare(TooHigh)==0)
{
if(currentAnswer<Highest)
Highest=currentAnswer;
}
else if (currentResult.compare(TooLow)==0)
{
if (currentAnswer>Lowest)
Lowest=currentAnswer;
}
else if (currentResult.compare(Bingo)==0)
{
if (currentAnswer>=Highest||currentAnswer<=Lowest)
{
cout<<dishonest<<endl;
}
else
cout<<honst<<endl;
Highest=11;
Lowest=0;
}
}
} while (currentAnswer!=0);
return 0;
}
解题错误:
1.cin>>string报“>>操作符没有被重载”的错误:
错误原因挺郁闷人的,因为江苏省比赛的时候用的是C语言,结果习惯的#include<string.h>。
其实这是错误的,既然是C++,就应该#include <string>,然后using std::string。
这样就成功解决了错误。
2.cin>>int后getLine(string)程序死锁:这个错误也是很典型的,原因就是万恶的缓冲器。cin输入一个数字后有个回车是没有读入的,
但是缓冲区还留着这个回车,所以getline读到的就是这个回车。一轮循环后读cin>>int时读到
的却是一个字符串……这下程序就陷入了死锁啦。
解决办法也挺简单,只要在cin>>int后加入一个cin.get(),吸掉这个回车就可以啦。
我记得当时上C语言课的时候,周伟老师也提过这个问题的。
3.Wrong Answer:当时看到很多人用数组做,想法很好,但是我觉得不必要用数组,一个highest,一个
lowest,一个current足以。所以就做下去,第一版代码如下:
有兴趣的读者可以先别看前面的代码,调试下这个程序到底错在什么地方。如果不知道
的可以试试下面这组测试数据(dongshanluo 提供):
6 too high 8 too high 7 right on问题来了……上次的Highest被刷掉了……果然还是考虑不严密啊。经过改正后,程序成功
ACCEPTED。
[题外话]最近开始研究硬盘技术(包括磁盘格式),非常累人,所以博客也写得少了。最近去了微
软公司一个员工的博客,发现虽然他从事的工作和算法关系不是很大,但是他还是经常去uVa
做题。最后我还是发现,不论是从事何种编程工作,只要你还想升职,算法对你的帮助是非常
大的。微软,Google找的都是算法精英。所以有空的话还是把算法学学好。
这学期有几门课实在是教的不怎么样,但偏偏很重要,比如《算法分析》和《计算机组成结
构》,简直是捣浆糊。《微机原理与汇编》虽然老师并不是讲的很透,但是我花了双倍的时间,
加上我对程序的天赋(现在还没涉及到算法),总算学的不错。但是我已经对我的学校彻底无
语了……
这学期的寒假目标基本上已经定下来了:
1.算法分析(算法导论也该开始看了……还有<<编程之美>>,都要积灰了)2.数据库设计(数据库原理是这学期上得最好的一门课,简直是完美,不愧是缪副院长,寒假的
时候要好好把表的分解,设计那部分看看)
3.C++(学了两年了,越来越不会了……这学期学了汇编,终于可以C++和汇编混着用,也终于可
以看<<The art of computer programming>>了)