python 牛客)多行輸入輸出

2021-10-09 05:29:47 字數 1651 閱讀 1263

本文主要對使用牛客做筆試的的輸入輸出的問題做個總結

多行輸入(在第一行標註行數)

形如:

5

a bn l

c oo k

m f

import sys

s =int

(sys.stdin.readline())

# s = int(input()) # 二者皆可

ans =

for i in

range

(s):

#if s != ' ':

l =input()

.split(

) temp =

[j for j in l]

print

(ans)

**實際輸出:[

['a', 'b'

], [

'n', 'l'

], [

'c', 'o'

], [

'o', 'k'

], [

'm', 'f'

]]

2.多行輸入,行數未知

形如:

abc

123456789

34445

sdff

將其轉化為陣列*

第一步:先將字串轉為列表

第二步:再把列表中的元素逐個加入到要輸出的列表中

第三步:就按需求輸出了

```python

import sys

res =

while true:

try:

# 按字串讀入一行,

s = str(input(

))# 兩次轉列表

''.join(list(map(str,s.split(

' ')))))

except:

break

# 將其轉為列表

print(res)

# 將其單行輸出

print(

' '.join(res))

實際輸出:

[

'abc', '123456789', '34445', 'sdff'

]abc 123456789 34445 sdff

1.兩行數字

249

23

**:

while

true

:try

: s =

list

(map

(int

,input()

.split(

" ")))

s1 =

list

(map

(int

,input()

.split(

" ")))

except

:break

print

(s)print

(s1)

實際輸出:

[2,

4,9]

[2,3

]

牛客網js單行多行輸入輸出

單行輸入v8 a b的值 readline得到的是字串,本例需要轉化為數字 while line readline rl.on line function line 輸入1 5 輸出6 node 多行 輸入行數確定 題目 給定兩個字串s1和s2,合併成乙個新的字串s。合併規則為,s1的第乙個字元為s...

牛客輸入輸出總結

重大失誤 注意,input這個函式就是寫一次輸入一次,出現多少個代表輸入多少次。輸入進來被當為字串。a input b input print a print true print b aba true b雖然input 可以解決,但是還是補上 sys.stdin.readlines 讀取一整行 i...

牛客網 python輸入輸出要求

牛客網推薦使用sys.stdin.readline 形式輸入,輸出用print 牛客網的一般程式設計題和劍指offer與letcode的輸入輸出格式不同 如 計算兩數的和 python2 獲取輸入 import sys try while true line sys.stdin.readline s...