Study/Baekjoon
[Python] 백준 10951 | while, for loop
yeorii
2023. 3. 11. 23:36
# 10951. A+B - 4
문제
두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.
입력
입력은 여러 개의 테스트 케이스로 이루어져 있다.
각 테스트 케이스는 한 줄로 이루어져 있으며, 각 줄에 A와 B가 주어진다. (0 < A, B < 10)
출력
각 테스트 케이스마다 A+B를 출력한다.
예제 입력 1
1 1
2 3
3 4
9 8
5 2
예제 출력 1
2
5
7
17
7
내 풀이
import sys
while True:
try :
inp = input()
print(sum(map(int, inp.split())))
if inp=="":
break
except :
break
난 while 쓰고 싶었고,
`while True:` 무한 루프를 걸어서 input에 아무것도 안들어오거나 EOFError가 나올 경우 break 걸리게 했다... 코드 왕 길지만TT
다른 풀이 - for 활용
sys.stdin.readlines() 활용!
import sys
lines = sys.stdin.readlines()
for line in lines:
print(sum(map(int, line.split())))
sys.stdin 활용!..
import sys
for x in sys.stdin:
a, b = map(int, x.split())
print(a+b)