简单尺取

题目:
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.

题目大意:
Obs:这是一个互动问题。更多信息在“交互”部分下。

MaratonIME正在聚集,开始另一项小组练习。这次,伦佐(Renzo)决定奖励学生解决问题的糖果。MaratonIME的成员虽然好奇,却开始猜测Renzo带了多少糖果。对于每个问题,Renzo都回答了糖果的数量是否更高,更低或等于所要求的数量。

Breno注意到糖果的数量可能很高,因此决定将查询数量限制为50个。这样,练习将尽快开始。

伦佐(Renzo)至少购买了1个,最多不超过10个9个糖果。查找不超过50个问题的Renzo购买了多少糖果。

题解:
这题怎么说呢, 用二分很容易就可以做出来,但是也很容易出错。我们只要设左右两个边缘数据,用二分的方法,结合题意很容易就可以了,很水。

代码如下:

#include<iostream>
#include<cstdio>

using namespace std;

int main()
{
	int left=1,right=1e9;
	char ch;
	while(left<=right)
	{	
		int mid=left+(right-left)/2;
		printf("Q %d\n",mid);
		cout.flush();
		cin>>ch;
		if(ch=='<') right=mid-1;
		else if(ch=='>') left=mid+1;
		else if(ch=='=') break;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值