ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

H - 二分+交互 Gym - 101375H

2020-01-13 22:03:27  阅读:303  来源: 互联网

标签:二分 candies Gym Renzo question flush output 101375H


H - 二分+交互

Gym - 101375H

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 1e9 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.

Example

Input

>
<
=

 

Output

Q 1
Q 3
Q 2

 

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!

题目描述:

Renzo带的糖可能是1~1e9。猜他带了多少糖,多了少了会给相应的符号,猜中就结束程序。

分析:

二分,关键是记得清除缓存和二分区间1~1e9。在这个区间内二分即可。

代码:

#include<stdio.h>

using namespace std;
int main()
{
    char c;
    int l=1;
    int r=1e9;
    int mid;
    while(l<=r)
    {
        mid=(l+r)/2;
        printf("Q %d\n",mid);
        fflush(stdout);
        c=getchar();
        getchar();
        if(c=='<')
        {
            r=mid-1;
        }
        else if(c=='>')
        {
            l=mid+1;
        }
        else
        {
            break;
        }
    }
    
    return 0;
}

标签:二分,candies,Gym,Renzo,question,flush,output,101375H
来源: https://www.cnblogs.com/studyshare777/p/12189562.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有