python在线oj输入输出

## 数字输入问题
# 只获取一个输入数字时,由于输入的格式都是字符类型,所以要用int转化
n = int(input())

# 来一行数,输出一个结果,用try和except来实现,
while 1:
    try:
        a, b = map(int, input().split())
        print(a+b)
    except:
        break

# 直接都输入进来,把数据都存入data中后,再逐个处理并输出
import sys
data = []
for line in sys.stdin.readlines():
    # 存入的时候直接把数据格式也转为int型
    data.append(list(map(int, line.strip().split())))
for d in data:
    print(sum(d[1:]))

# 先把字符串输入进来,再转换
cou = int(input())
data = []
import sys
for line in range(cou):
    # 这里的strip()不能丢,为了去除空格
    data.append(input().strip().split())
for d in data:
    a = []
    for i in d:
        a.append(int(i))
    print(sum(a[1:]))

# 字符串输入问题
# input()函数用于读取一行
n = int(input())
a = input().split()
# a是list类型
print(type(a), a)
a.sort()
for i in range(len(a) - 1):
    # print的end参数用于在输出的末尾加空格,end参数默认是换行,
    print(a[i], end=" ")
print(a[len(a) - 1], end="")

# 多行输入时要用try-except结构
while 1:
    try:
        a = input().split(',')
        a.sort()
        l = len(a)
        for i in range(l-1):
            print(a[i], end=',')
        print(a[-1])
    except:
        break
View Code

参考:https://blog.csdn.net/qq_39938666/article/details/101004633

https://blog.csdn.net/weixin_41789280/article/details/105020103

2 牛客常见报错

1> 报输出为空,一定是输出输出数据的问题,下面这个将input()写出了intput()

while 1:
    try:
        l = int(input())
        #data = list(map(int, intput().strip().split()))
        data = list(map(int, intput().split()))
        res = [1] * l
        stack = []
        for i in range(l):
            k = len(stack)
            while stack and data[stack[-1]] <= data[i]:
                stack.pop()
            res[i] += k
            stack.append(i)
        stack = []
        for j in range(l-1, -1, -1):
            k = len(stack)
            while stack and data[stack[-1]] <= data[j]:
                stack.pop()
            res[j] += k
            stack.append(j)
        str_res = ' '.join([str(i) for i in res])
        print(str_res)
    except:
        break
View Code

常见报错:https://www.nowcoder.com/discuss/276

posted on 2020-03-29 17:32  吃我一枪  阅读(1283)  评论(0编辑  收藏  举报

导航