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
【分析】:分别在too high和too low时比较数字的大小,选出最小low值和最大high值,比较right on说出的数字合法(在low和high范围内为合法)与否。注意置位!

【代码】:
#include <iostream>
#include<string>
#include<algorithm>
#include<cstdio>
using namespace std;
#define oo 10000000
int n;
char a[500];
/*模拟二分*/
int main()
{
    int low=-1,high=oo;
    while(~scanf("%d",&n),n)
    {
        getchar();
        gets(a);
        if(a[4]=='h')
        {
            if(high>n)
                high=n;
        }
        if(a[4]=='l')
        {
            if(low<n)
                low=n;
        }
        if(a[0]=='r')
        {
            if(n<high&&n>low)
            {
                puts("The guy may be honest");
            }
            else
            {
                puts("The guy is dishonest");
            }
            low=-1,high=oo;
        }
    }
}
模拟

  

原文地址:https://www.cnblogs.com/Roni-i/p/8013159.html