算法刷题汇总 python版本

OJ在线编程常见输入输出练习

牛客网练习链接:https://ac.nowcoder.com/acm/contest/5657#question

1.读取行数未知

方法一:使用for line in sys.stdin

import sys
 
for line in sys.stdin:
    a = line.split()  # split()默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等
    print(int(a[0]) + int(a[1]))

方法二:使用try + while

try:
    while True:
        a= input().split(" ")
        print(int(a[0])+int(a[1]))
except:
    pass

2.数字list转字符串

# input() = "c d a bb e"
a = input().split()   #   a  = ['c', 'd', 'a', 'bb', 'e']
data = sorted(a)      # data = ['a', 'bb', 'c', 'd', 'e']
out = " ".join(data)  #  out = "a bb c d e"
print(out)

3.注意每行输入都有换行符

import sys

for line in sys.stdin:

你可能感兴趣的:(python,算法,leetcode)