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


免責聲明!

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



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