FZU-1575
原题链接
原题:某天,无聊的小斌叫上几个同学玩游戏,其中有比较笨的小兴,比较傻的小雪,可爱的小霞和自以为是的小楠。他们去找聪明的小明去给他们当裁判。判定谁取得游戏胜利。
而这个游戏是由小斌想个1到10000000的数字让大家猜,看谁先猜中。为了防止小斌作弊,小明记录下了游戏的整个过程。你的任务是判断小斌是否有作弊。
Input
输入数据包括多盘游戏。一次猜数包含两行,第一行是一个数字n(1<=n<=10000000),表示所猜数字。第二行是小斌的回答为"too high",“too low”,“right on"三种答案之一。每盘游戏结束于"right on”。当n=0的时候,整个游戏结束。
Output
对于每盘游戏,若小斌确有撒谎,请输出一行"The guy is dishonest",否则请输出"The guy may be honest"。
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
The guy is dishonest
The guy may be honest
题意:
进行多局游戏,对于每一局游戏的结果进行判定是否正确,若不正确则输出“The guy is dishonest”,若正确输出“The guy may be honest”;
题解:
水题一道,但是由于很久没写题目了,还卡了一下。
主要思想就是设置一个最低值low=0和最高值high=10000000,每次输入字符串时判断若是too high ,则将high与n比较,给high重新赋两者之间的最小值,low一样。最后比较结果是否在low和high之间即可。
注意:结果不能等于low和high。
附上代码:
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
int n,high,low,res;
string a;
int main()
{
n=-1;
while(n!=0)
{
high=10000000;
low=0;
res=0;
while(1)
{
cin>>n;
getchar();
if(n==0) break;
getline(cin,a);
if(a=="right on") {res=n;break;}
else if(a=="too high") high=high<n?high:n;
else if(a=="too low") low=low>n?low:n;
}
if(n==0)break;
if(res<=low||res>=high)cout<<"The guy is dishonest"<<endl;
else cout<<"The guy may be honest"<<endl;
}
return 0;
}