포스트

[Python] Baekjun No. 10951 - A+B - 4

We have summarized how to handle the A+B problem with an undetermined number of inputs by repeating sys.stdin.

한국어 원문은 여기에서 볼 수 있습니다.
[Python] Baekjun No. 10951 - A+B - 4

BaekJoon 10951

Solution

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:

  • Repeats the entire input and stores the entire input in line
  • Can be used when the number of inputs is not fixed
  • a, b = map(int, line.split()) -> Divide the entire input received by space and save as int

Incorrect (old) code

1
for line in sys.stdin.readline()
  • When you do this, you read ‘one line’ and repeat each character within that line.

Other methods

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)

If there is no more input, the while loop ends.