[zoj] 1001. A + B Problem

题目

Calculate a + b

Input

The input will consist of a series of pairs of integers a and b,separated by a space, one pair of integers per line.

Output

For each pair of input integers a and b you should output the sum of a and b in one line,and with one line of output for each line in input.

Sample Input

1 5

Sample Output

6

Hint

Use + operator

思路

ob功能测试题。。。给出多行输入,每行包括两个数字,输出两数相加的结果。如果真要说有难点,那么就在于如何判断输入结束,即判断eof,c、c++中能够通过scanf或cin的返回值是否为0判断,而python的input函数遇到eof则直接会抛出异常,所以要么使用try处理异常,要么使用sys.stdin.readlines方法,把标准输入当成文件使。

代码

python版本:

# 1. sys.stdin.readlines
import sys

lines=sys.stdin.readlines()
for nums in lines:
    nums=[int(i) for i in nums.split()]
    print(sum(nums))

# 2. 处理异常
while True:
    try:
        nums=input().split()
    except Exception:
        break
    nums=[int(i) for i in nums]
    print(sum(nums))

posted @ 2022-02-14 15:16  frankming  阅读(103)  评论(0编辑  收藏  举报