공부/코딩테스트
파이썬 빠른 입출력(readline, write)
디티
2025. 1. 31. 19:55
문제의 입력이나 출력이 많아질 경우
파이썬은 시간초과가 발생할 수 있다.
sys를 import 해주고
input 대신 sys.stdin.readline()
print 대신 sys.stout.write()를 사용하자
sys.stdin.readline()은 개행문자도 같이 받아오니 주의(입력후 엔터 "\n"도 입력으로 받아온다)
sys.stdin.readline().strip()으로 개행문자를 제거 해줄수 있다
사용 예시
import sys
N = int(sys.stdin.readline())
for i in range(N):
command = sys.stdin.readline().strip()
if command[:4] == 'push': # push
X = int(command[5:])
command = command[:4]
que_state.push(X)
elif command[:] == 'front': # front
sys.stdout.write(f"{que_state.front()}\n")