MaratonIME gets candies/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 109 candies. Find how many candies were bought by Renzo with no more than 50 questions.
题目来源:Gym - 101375H
题目链接
题意
交互型的题,需要你猜一个数,你每次可以询问一个数,然后会给你三种结果,>,<,=,>就是你询问的数比正确答案大,>就是你询问的数比正确答案小,=就是你询问的数是正确的,最多只能问50次。
解法
二分,每次询问mid,如果是>就找左边,是<就找右边,知道找到为止。
代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <windows.h>
using namespace std;
char st;
int main()
{
int L=1,R=1000000000;
while (L<=R)
{
int mid=(L+R)/2;
printf("Q %d\n",mid);
cout.flush();
cin>>st;
if (st=='>') L=mid+1; else
if (st=='<') R=mid-1; else
if (st=='=') break;
}
return 0;
}