交互器比赛版
# interactor.py
import subprocess
import sys
N = 1_000_000_000
SECRET = 734_211_123
LIMIT = 60
# Windows: py interactor.py .\main.exe
# Linux: python3 interactor.py ./main
p = subprocess.Popen(
sys.argv[1:],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
text=True,
bufsize=1,
)
def send(x):
p.stdin.write(f"{x}\n")
p.stdin.flush()
def fail(msg):
p.kill()
p.wait()
raise SystemExit("FAIL: " + msg)
send(N)
queries = 0
while True:
a = p.stdout.readline().split()
if not a:
fail("程序提前结束")
if a[0] == "?":
if len(a) != 2:
fail("查询格式错误")
x = int(a[1])
if not 1 <= x <= N:
fail("查询越界")
queries += 1
if queries > LIMIT:
fail("超过询问次数")
# 返回:SECRET <= x ? 1 : 0
send(int(SECRET <= x))
elif a[0] == "!":
if len(a) != 2:
fail("答案格式错误")
answer = int(a[1])
print(
"AC" if answer == SECRET else f"WA: answer={answer}, secret={SECRET}",
file=sys.stderr,
)
p.kill()
p.wait()
break
else:
fail("无法识别的输出")
haze

浙公网安备 33010602011771号