1. Python處理單行輸入
輸入:1 2 輸出:3 x = list(map(int, input().split(" "))); # 注意:Python 3.x中,map()的返回值是迭代器、並不是列表 print(sum(x));
2. Python處理多行輸入
輸入:1 2
3 4
輸出:3
7
while True:
try:
x = list(map(int,input().split(" "))); # 注意:Python 3.x中,map()的返回值是迭代器、並不是列表
print(sum(x));
except:
break;
當題目沒有給出「總共要處理的輸入行數」或是「停止處理輸入的條件」時,處理輸入時就需套下述模板:
while True: try: # 讀取單行輸入的代碼 # …… …… except: break;
3. Python處理 t 行輸入
輸入描述: 第一行是一個數據組數t 接下來每行包括兩個正整數a, b 輸出描述: a+b的結果 輸入:2 1 5 10 20 輸出:6 30 t = int(input()); for i in range(t): x = list(map(int,input().split(" "))); print(sum(x));
