[Gym - 101375H]MaratonIME gets candies(交互+二分)

17 篇文章 0 订阅
10 篇文章 0 订阅

题目传送门:Here
Time limit per test: 2.0 s
Memory limit per test: 256 MB
Input: standard input
Output: standard output

Description

Obs: this is an interactive problem. More information is under the “Interaction” section.


MaratonIME is gathering to start another group practice. This time,Renzo decided to reward the students with candies as they solve problems. Curious as they are, the members of MaratonIME started to guess how many candies did Renzo bring. For each question, Renzo answered if the amount of candies was higher, lower or equal to the number asked.


Breno, noticing that the amount of candies could be very high, decided to limit the number of queries to 50. This way, the practice would start as soon as possible.


Renzo bought at least 1 and no more than 109 candies. Find how many candies were bought by Renzo with no more than 50 questions.

Input

For every question asked, read a character. It will be " > " if the amount of Renzo’s candies is higher than your guess, " < " if the amount of Renzo’s candies is lower than your guess or " = " if your guess is equal to the amount of Renzo’s candies.

Output

You must print every query in the format “Q y”, where y is the number guessed, and 1 
≤ y ≤ 109. After printing a question, you need to flush the output.
Check the “Interaction” section for examples of how to flush the
output.

Interaction
To ask Renzo a question, print the query in the format above. After printing a question, you need to flush the output. See examples below for each language:

C: fflush(stdout)

C++: cout.flush()

Java: System.out.flush()

Python: sys.stdout.flush()

Pascal: flush(output)

After each query, read a character as described above.


As soon as Renzo answers your question with “=” the program has to terminate. Your answer will be considered correct if you asked no more than 50 questions.

Input

>
<
=

Output

Q 1
Q 3
Q 2

样例分析:
输出Q 1 电脑回答>
输出Q 3 电脑回答<
输出Q 2 电脑回答=
程序结束
(先printf再scanf)

Note
In the example, the number of candies is 2. The answer is considered to be correct because only 3 out of the 50 possible questions were asked to obtain the character " = ".
Make sure to flush the output after printing each question!


解析
一个猜数字游戏。非常有趣的交互题目,你猜电脑给的是什么数字,然后电脑回答你他的数(answer)和你的数(guess)的关系。而且必须50次内得到正确的结果,采用二分法可以快速缩小范围。注意最后一句话,每次提问之后要刷新一次(等问答嘛),每种语言的刷新方式都有给。


**C语言代码**
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
    char judge;
    int i,l=1,r=1e9,mid;
    for(i=0;i<=50;i++)
    {
        mid=(r+l)/2; //二分走起!
        if(l<r)
            printf("Q %d\n",mid);
        else printf("Q %d\n",r);
        cout.flush();
        scanf("%c",&judge);
        getchar();
        //记得清换行 不然下一个judge就读了
        //我就是没有清一直WA  发现问答次数少了一半(滑稽)
        if(judge=='>')
            l=mid+1;
        else if(judge=='<')
            r=mid-1;
        else if(judge=='=')
            break;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值