[Python] 백준 10951번 - A+B - 4
입력 개수가 정해지지 않은 A+B 문제를 sys.stdin 반복으로 처리하는 방법을 정리했습니다.
For the English version of this post, see here.
[Python] 백준 10951번 - A+B - 4
풀이
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 반복문을 종료시킴