多行多组输入1
res = []
n = int(input())
for _ in range(n):
try:
line = input()
except:
break
a,b = line.split()
res.append(int(a)+int(b))
for i in range(len(res)):
print(res[i])
多行多组输入2
res = []
while True:
try:
line = input()
except:
break
if line != "":
a,b = line.split()
res.append(int(a)+int(b))
else:
break
for i in range(len(res)):
print(res[i])
多行多组输入3
即以特定行结束输入,只需要在多行多组输入2的基础上改一下终止判断即可
res = []
while True:
try:
line = input()
except:
break
if line != "0 0":
a,b = line.split()
res.append(int(a)+int(b))
else:
break
for i in range(len(res)):
print(res[i])
多行多组输入4
这里每行不确定多少个数字,可以先转换为数组
res = []
while True:
try:
line = input()
except:
break
if line != "0":
line_arr = [int(i) for i in line.split()]
line_sum = sum(line_arr[1:])
res.append(line_sum)
else:
break
for i in range(len(res)):
print(res[i])
参考
OJ在线编程常见输入输出练习场
|