#BS0000109. Đoán số bí mật (Guess Number Higher or Lower)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đoán số bí mật (Guess Number Higher or Lower)
Guess Number Higher or Lower
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
The judge chooses a secret integer in , fixed for the entire game. You may guess a number and learn whether is higher, lower, or equal to it. Find . The original LeetCode problem uses the guess(num) API; this edition uses an interactive message protocol.
Input
The judge first sends integer . The secret number is not provided. Each query is answered by -1, 0, or 1 on a new line.
Output
Print ? x (). The judge replies -1 if , 1 if , and 0 if . When ready, print ! p and stop. Flush after each message. At most 31 queries are allowed.
Interactive Protocol
Lines beginning with ? are queries to the judge; lines beginning with ! are final answers. Do not print any other text to standard output.
Subtasks
- Subtask 1 (20 points): ; is the hidden secret number.
- Subtask 2 (30 points): ; is the hidden secret number.
- Subtask 3 (50 points): ; is the hidden secret number.
Examples
Input
10
Output
? 5
? 8
? 6
! 6
This is an illustrative transcript, not a fixed output file. Hidden values are revealed only in the explanation to make the example understandable.
Explanation
Suppose and . Query : reply 1, so the secret is in . Query : reply -1, so it lies in . Query : reply 0; finish with ! 6. This is only an example transcript.