포스트

[Python] 백준 10951번 - A+B - 4

Python 입력 개수 미지정 시 sys.stdin 활용, 파일의 끝(EOF)까지 안전하게 읽어오는 기법

For the English version of this post, see here.
[Python] 백준 10951번 - A+B - 4

BaekJoon 10951

풀이

1
2
3
4
5
import sys

for line in sys.stdin:
    a, b = map(int, line.split())
    print(a+b)

for line in sys.stdin:

  • 전체 입력을 반복하고, line에 그 전체 입력이 저장됨
  • 입력 개수가 정해져있지 않을 때 사용 가능
  • a, b = map(int, line.split()) -> 받아온 전체 입력을 공백 기준으로 나누고, int로 저장

틀린(이전) 코드

1
for line in sys.stdin.readline()
  • 이렇게 하게 되면 ‘한 줄’을 읽어서 그 한 줄 안에 문자를 하나씩 반복하는 것

다른 방법

1
2
3
4
5
6
7
8
9
10
import sys

while True:
	line = sys.stdin.readline()

	if not line:
    	break
	else:
   	 a, b = map(int, line.split())
    	print(a+b)

입력이 더이상 없다면 while 반복문을 종료시킴

댓글

궁금한 점, 피드백, 오류 제보를 남겨 주세요.