banner
NEWS LETTER

Python交互题模板

Scroll down

答题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import sys
def output(s):
sys.stdout.write(s + "\n")
sys.stdout.flush()


def answer(*args):
output("! " + " ".join(map(str, args)))


def query(*args):
output("? " + " ".join(map(str, args)))
return input()

交互器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import subprocess


class Interactor:
def __init__(self):
self.init_output = ""

def response(self, query: str): ...

def start(self, cmd):
with subprocess.Popen(
cmd,
text=True,
stdin=-1,
stdout=-1,
) as p:
if self.init_output:
p.stdin.write(self.init_output + "\n")
p.stdin.flush()
while resp := self.response(p.stdout.readline()):
p.stdin.write(resp + "\n")
p.stdin.flush()

我很可爱,请给我钱

其他文章
目录导航 置顶
  1. 1. 答题
  2. 2. 交互器