1H: MaratonIME gets candies

题目来源

题目

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.

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!

题意

Obs:这是一个互动问题。更多信息在“交互”部分下。
MaratonIME正在聚集,开始另一项小组练习。这次,伦佐(Renzo)决定奖励学生解决问题的糖果。 MaratonIME的成员虽然好奇,却开始猜测Renzo带了多少糖果。对于每个问题,Renzo都回答了糖果的数量是否更高,更低或等于所要求的数量。
Breno注意到糖果的数量可能很高,因此决定将查询数量限制为50。这样一来,这种做法将尽快开始。
伦佐(Renzo)至少购买了1个,最多不超过109个糖果。查找不超过50个问题的Renzo购买了多少糖果。

输入项
对于每个提出的问题,请阅读一个字符。如果Renzo的糖果量大于您的猜测,则为“>”;如果Renzo的糖果量小于您的猜测,则为“ <”;如果您的猜测等于Renzo的糖果量,则为“ =”。

输出量
您必须以“ Q y”格式打印每个查询,其中y是猜测的数字,且1≤y≤109.打印问题后,需要刷新输出。检查“交互”部分,以获取有关如何刷新输出的示例。

相互作用
要问Renzo一个问题,请按上述格式打印查询。打印问题后,您需要刷新输出。请参阅以下每种语言的示例:
C:fflush(标准输出)
C ++:cout.flush()
Java:System.out.flush()
Python:sys.stdout.flush()
帕斯卡:冲洗(输出)
每次查询后,请按上述说明读取一个字符。
一旦Renzo用“ =”回答您的问题,程序就必须终止。如果您提出的问题不超过50个,则您的答案将被认为是正确的。

输入项

<

输出量

Q 1
Q 3
Q 2

注意
在该示例中,糖果的数量为2。答案被认为是正确的,因为在50个可能的问题中,只有3个被要求获得字符“ =”。
确保在打印每个问题后刷新输出!

解法

其实只要二分答案,注意一下交互式的处理就行了(我才不会告诉你我WA了7次

#include <iostream>
#include <cstdio>

int main(){
 char ch;
 int left = 1;
 int right = 1e9;
 while(left<=right){
  int mid = left + (right-left)/2;
  printf("Q %d\n", mid);
  cout.flush();
  cin >> ch; 
  if(ch=='>') left = mid+1;
  else if(ch=='<') right = mid-1;
  else if(ch=='=') break; 
 }
 
 return 0;
}
发布了14 篇原创文章 · 获赞 0 · 访问量 150

猜你喜欢

转载自blog.csdn.net/loaf_/article/details/103961830