F - 二分+交互 Gym - 101375H

本文介绍了一种使用二分法解决交互式问题的方法,具体为在限定次数内猜出Renzo购买的糖果数量,范围从1到10^9。通过不断缩小搜索范围,每次猜测中间值并依据反馈调整,最终准确找到目标值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

F - 二分+交互 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.

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)

用二分法做,取开头两端,不断取中间值,在靠近的一半中再取中间值,2^50>1e9,所以50次可行。
交互题,不一样的就是平常输入时这里变输出,输出变输入。记得还要用
fflush(stdout)刷新

#include<stdio.h>
int main(){
	int n,l,r,i;char m;
	n=1e9+1;r=n;l=0;
	for(i=0;i<50;i++){
		n=(l+r)/2;
		printf("Q %d\n",n);
		fflush(stdout);
		scanf("%c",&m);getchar();
		if(m=='<') r=n;
		if(m=='>') l=n;
		if(m=='=') break;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值