Python — 牛客輸入處理


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));

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM