Guess The Numbe (交互题)

D. Guess The Number
Time limit 2 seconds
Memory limit 512Mb
Input standard input
Output standard output

This is an interactive problem.

Somebody has secretly set the number x (1 ≤ x ≤ n). Your program has to guess this number interacting with the testing system. There are two kinds of queries:

? y (1 ≤ y ≤ n) — the result is either string “>=” which means that x ≥ y, or string “<” which means that x < y (without quotes)
! y (1 ≤ y ≤ n) — should be called once when your program is about to terminate. All output after it is ignored. You must make this call to tell the system the number you discovered (i. e., x = y).

Interaction Protocol

Input starts with a line with single integer 1 ≤ n ≤ 109. After it there will follow separate lines with answers to your queries in form .

Output queries one per line, as specified in the statement.

After each query read the separate line with answer to your query in form ”? y”.

The total number of queries of the first type must not be greater than binary logarithm of n rounded up, otherwise you will get “Wrong answer”. Do not forget to do two things after each query: 1) to print the newline; 2) to flush the output. It is possible to do using fflush(stdout) or cout.flush() in C/C++ and flush(output) in Pascal.

Interaction Example

16
? 9
<
? 5

=
? 7
<
? 6

 ! 6

第一次做交互题,根据你的输入会不断反馈给你输出帮助你找到答案。这题很明显就是一个二分查找,注意一下二分过程 当<时 r=mid-1(答案必然小于mid) >=时 l=mid(答案可能等于mid) 然后当范围缩小到l==r时,l即为答案。且mid=(1+l+r)/2要不然会无限循环 L 3 R 4 mid=(3+4+1)/2=4就可以顺势让r-1=3,得出答案3。

啊对,一般交互题要注意每次输出后加个cout.flush() 来清空缓冲区,下面代码中cout<<endl也会清空所以就不加了。

Code

#include<iostream>
#include<string>
using namespace std;

int main()
{
   
	int n;cin >> n;
	int l = 1, r = n;
	while (l != r)
	{
   
		int mid = (l + r+1) / 2;
		cout << "?" << mid << endl;
		string str;cin >> str;
		if (str == "<")r = mid - 1;
		else l = mid;
	}
	cout << "?" << l << endl;
}

全部评论

相关推荐

03-26 13:04
已编辑
电子科技大学 算法工程师
xiaowl:你这个简历“条目上”都比较有深度性,但是实际上面试官又没法很好的评估你是怎么达到很多看上去很厉害的结果的。要避免一些看上去很厉害的包装,比如高效的内存复用策略的表达,如果仅是简单的一些内存共享机制,而且面试上也没有深挖的空间,就不要这样表达。比如,工程化模式本质上可能就是定义了一些abstract class,那也就没特别多值得讲的内容。建议简历上应该侧重那些你花了大量时间和精力解决、研究的问题,不要过分追求“丰富”,而是关注在技术深入度、问题解决能力的表现上。
没有实习经历,还有机会进...
点赞 评论 收藏
分享
头像
04-27 15:11
已编辑
华东师范大学 算法工程师
暑期实习从2月开始投,面了两个月,流程该挂的都挂完了,腾讯字节一共号称是1.7w个hc,不知道都发给谁了,估计今年秋招要难顶。Timeline米哈游、美团、蚂蚁、微软等公司直接简历挂穿,没进面。携程:3.3&nbsp;投递、测评3.12&nbsp;笔试3.18&nbsp;一面3.25&nbsp;二面4.13&nbsp;ai面(hr面)4.14&nbsp;英语测评4.23&nbsp;offer(已拒)腾讯:2.6&nbsp;测评2.28&nbsp;wxg一面3.5&nbsp;wxg二面(挂)3.11&nbsp;teg一面3.21&nbsp;teg二面(取消)3.31&nbsp;teg一面4.10&nbsp;teg二面(挂)4.21&nbsp;wxg一面4.24&nbsp;wxg二面(挂)字节:1.28&nbsp;aml约面(取消)3.17&nbsp;火山一面(挂)4.8&nbsp;aml一面(挂)4.20&nbsp;抖音data一面(挂)阿里:3.23&nbsp;投递、测评3.28&nbsp;笔试3.31&nbsp;淘天一面4.8&nbsp;钉钉一面4.9&nbsp;淘天二面4.10&nbsp;阿里控股一面4.12&nbsp;钉钉二面(取消)4.15&nbsp;淘天hr面4.16&nbsp;淘天offer(已接)4.21&nbsp;高德一面(取消)4.22&nbsp;淘宝闪购一面(取消)面试最大的感触是,现在撞上ai转型,一堆老业务急着转向,新业务非常不成熟,研究型的组bar非常高根本进不去,业务侧挂着算法的岗位干的都是工程活,面试却又要问算法,另外agent的落地也远没有那么广,绝大多数还是那套写死的系统调一下llm&nbsp;api或者做做rag,其余少部分真的在搭agent的,基本不能在线上服务用什么很智能的模型,现阶段成本太高,进去大概率就是给垃圾模型从工程方面兜底,除了业务场景的应用和数据经验以外,技术方面很难有什么提升。算法岗做不了基模的还是去搜广推好,之前判断失误了完全没投,秋招不知道还进不进得去。
嵌入式的小白:不错啊,淘天也是挺好的,恭喜
我的求职进度条
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务