OJ在線編程常見輸入輸出

OJ在線編程常見輸入輸出。
刷了算法題,結果發現筆試的時候,許多的輸入輸出是要自已定義的,故蒐集與總結一些 OJ在線編程常見輸入輸出 的輸入函數,並給出了在線編程的模板。

語言:python
OJ在線編程環境:牛客

地址
https://ac.nowcoder.com/acm/contest/320#question

獲取輸入

常用輸入獲取

t = list(map(int, input().strip().split()))
print(t)

map:使用方式,具體見

>>>def square(x) :            # 計算平方數
...     return x ** 2
... 
>>> map(square, [1,2,3,4,5])   # 計算列表各個元素的平方
[1, 4, 9, 16, 25]

strip(): 刪除輸入的兩端的空格
使用方式,具體見

         3 4 5 6
t: [3, 4, 5, 6]

split(): 爲空的時候,默認去除空格,可以是多個空格。
使用方式,具體見
如1

2              3               4              5
t: [2, 3, 4, 5]

split(" "):只能去除一個空格
如2

2       3  # 多個空格時候會報錯
Traceback (most recent call last):
	t = list(map(int, input().strip().split(" ")))
ValueError: invalid literal for int() with base 10: ''

模板,獲取輸入函數 -—— 數字輸入


def getInput(sep=None):
	'''
	:param sep:  split str.
	:return: a inpput list 
	'''
	if sep :
		return list(map(int, input().strip().split(sep)))
	else:
		return list(map(int, input().strip().split()))

模板,獲取輸入函數 -—— 字符/串輸入

def getInput(sep=None):
	'''
	:param sep:  split str.
	:return: a inpput list 
	'''
	if sep:
		return list((input().strip().split(sep)))
	else:
		return list((input().strip().split()))

模板,獲取輸入函數

def getInput(choice='intType',sep=None):
	'''

	:param choice: ['intType', 'strType']
		intType, to get int data
		strType, to get str data
	:param sep:  split str.
	:return: a inpput list
	'''

	if choice =='intType':
		if sep:
			return list(map(int, input().strip().split(sep)))
		else:
			return list(map(int, input().strip().split()))
	elif choice =='strType':
		if sep:
			return list((input().strip().split(sep)))
		else:
			return list((input().strip().split()))

模板,在線編程

def getInput(choice='intType',sep=None):
	'''

	:param choice: ['intType', 'strType']
		intType, to get int data
		strType, to get str data
	:param sep:  split str.
	:return: a inpput list
	'''

	if choice =='intType':
		if sep:
			return list(map(int, input().strip().split(sep)))
		else:
			return list(map(int, input().strip().split()))
	elif choice =='strType':
		if sep:
			return list((input().strip().split(sep)))
		else:
			return list((input().strip().split()))
        
try:
    
    while True:
        ### get in put 
        alist = getInput(choice ='strType', sep=',')
        
        ### operation about alist
        
        ### output your result
        
except:
    pass

在線編程


牛客 ——- 數字


測試題1:計算a+b


鏈接:https://ac.nowcoder.com/acm/contest/320/A
來源:牛客網

題目描述 
計算a+b

輸入描述:
輸入包括兩個正整數a,b(1 <= a, b <= 10^9),輸入數據包括多組。

輸出描述:
輸出a+b的結果

示例1
輸入
1 5
10 20
輸出
6
30
  • 代碼
try:
	while 1:
		a, b = list(map(int, input().split(" ")))
		print(a + b)
except:
	print("exit")
	pass

如果輸入的測試數據多個空格,那麼報錯,退出
如:

正常輸入
1 5

異常輸入
1              5

測試題2:計算a+b

鏈接:https://ac.nowcoder.com/acm/contest/320/B
來源:牛客網

題目描述 
計算a+b

輸入描述:
輸入第一行包括一個數據組數t(1 <= t <= 100)
接下來每行包括兩個正整數a,b(1 <= a, b <= 10^9)

輸出描述:
輸出a+b的結果
示例1
輸入
2
1 5
10 20

輸出
複製
6
30

代碼

測試題2:計算a+b


鏈接:https://ac.nowcoder.com/acm/contest/320/B
來源:牛客網

題目描述 
計算a+b

輸入描述:
輸入第一行包括一個數據組數t(1 <= t <= 100)
接下來每行包括兩個正整數a,b(1 <= a, b <= 10^9)

輸出描述:
輸出a+b的結果

示例1
輸入
2
1 5
10 20

輸出
6
30

代碼



try:
    t = list(map(int, input().strip().split()))

    for i in range(t[0]):
        # a, b = list(map(int, input().split(" ")))
        a, b = list(map(int, input().split()))
        print(a + b)

except:
	print("exit")
	pass
    
    



測試題3:計算a+b


鏈接:https://ac.nowcoder.com/acm/contest/320/C
來源:牛客網

題目描述 
計算a+b

輸入描述:
輸入包括兩個正整數a,b(1 <= a, b <= 10^9),輸入數據有多組, 如果輸入爲0 0則結束輸入

輸出描述:
輸出a+b的結果

示例1
輸入
1 5
10 20
0 0

輸出
6
30


while 1:
    a, b = list(map(int, input().strip().split()))
    if a+b == 0:
        break
    print(a+b)

測試題4:計算a+b

鏈接:https://ac.nowcoder.com/acm/contest/320/D
來源:牛客網

題目描述 
計算一系列數的和

輸入描述:
輸入數據包括多組。
每組數據一行,每行的第一個整數爲整數的個數n(1 <= n <= 100), n爲0的時候結束輸入。
接下來n個正整數,即需要求和的每個正整數。

輸出描述:
每組數據輸出求和的結果

示例1
輸入
4 1 2 3 4
5 1 2 3 4 5
0

輸出
10
15
while 1:
    a = list( map(int,input().strip().split()) )
    if a[0] == 0:
         break
    print(sum(a[1:]))
             

測試題5:計算a+b

鏈接:https://ac.nowcoder.com/acm/contest/320/E
來源:牛客網

題目描述 
計算一系列數的和

輸入描述:
輸入的第一行包括一個正整數t(1 <= t <= 100), 表示數據組數。
接下來t行, 每行一組數據。
每行的第一個整數爲整數的個數n(1 <= n <= 100)。
接下來n個正整數, 即需要求和的每個正整數。

輸出描述:
每組數據輸出求和的結果

示例1
輸入
2
4 1 2 3 4
5 1 2 3 4 5

輸出
10
15

測試題6:計算a+b

鏈接:https://ac.nowcoder.com/acm/contest/320/F
來源:牛客網

題目描述 
計算一系列數的和

輸入描述:
輸入數據有多組, 每行表示一組輸入數據。
每行的第一個整數爲整數的個數n(1 <= n <= 100)。
接下來n個正整數, 即需要求和的每個正整數。

輸出描述:
每組數據輸出求和的結果

示例1
輸入
4 1 2 3 4
5 1 2 3 4 5

輸出
10
15

try:
    while 1:
        a = list(map(int, input().strip().split()))
        print(sum(a[1:]))
except:
    pass

測試題7:計算a+b

鏈接:https://ac.nowcoder.com/acm/contest/320/G
來源:牛客網

題目描述 
計算一系列數的和

輸入描述:
輸入數據有多組, 每行表示一組輸入數據。
每行不定有n個整數,空格隔開。(1 <= n <= 100)。

輸出描述:
每組數據輸出求和的結果

示例1
輸入
1 2 3
4 5
0 0 0 0 0

輸出
6
9
0

try:
    while True:
        a = list( map(int, input().strip().split()))
        print(sum(a))
except:
    pass

測試題7:計算a+b,使用模板

def getInput(choice='intType',sep=None):
	'''

	:param choice: ['intType', 'strType']
		intType, to get int data
		strType, to get str data
	:param sep:  split str.
	:return: a inpput list
	'''

	if choice =='intType':
		if sep:
			return list(map(int, input().strip().split(sep)))
		else:
			return list(map(int, input().strip().split()))
	elif choice =='strType':
		if sep:
			return list((input().strip().split(sep)))
		else:
			return list((input().strip().split()))
        
try:
    while True:
        # a = list( map(int, input().strip().split()))
        a = getInput()
        print(sum(a))
except:
    pass

牛客 ——- 字符串


字符串排序1

鏈接:https://ac.nowcoder.com/acm/contest/320/H
來源:牛客網

題目描述 
對輸入的字符串進行排序後輸出

輸入描述:
輸入有兩行,第一行n
第二行是n個空格隔開的字符串


輸出描述:
輸出一行排序後的字符串,空格隔開,無結尾空格
示例1
輸入
5
c d a bb e

輸出
a bb c d e
try:    
    while True:
        n = list(map(int, input().strip().split()))
        s = list((input().strip().split()))
        s.sort()
        print(' '.join(s))
except:
    pass

字符串排序2

鏈接:https://ac.nowcoder.com/acm/contest/320/I
來源:牛客網

題目描述 
對輸入的字符串進行排序後輸出

輸入描述:
多個測試用例,每個測試用例一行。

每行通過空格隔開,有n個字符,n<100

輸出描述:
對於每組測試用例,輸出一行排序過的字符串,每個字符串通過空格隔開


示例1
輸入
a c bb
f dddd
nowcoder

輸出
a bb c
dddd f
nowcoder
try:
	while True:
		# n = getInput()
		s = list((input().strip().split()))
		s.sort()
		print(' '.join(i for i in s))  # 方式一
		# print(' '.join(s))  		   # 方式二
except:
	pass

字符串排序3

鏈接:https://ac.nowcoder.com/acm/contest/320/J
來源:牛客網

題目描述 
對輸入的字符串進行排序後輸出

輸入描述:
多個測試用例,每個測試用例一行。
每行通過,隔開,有n個字符,n<100

輸出描述:
對於每組用例輸出一行排序後的字符串,用','隔開,無結尾空格


示例1
輸入
a,c,bb
f,dddd
nowcoder

輸出
a,bb,c
dddd,f
nowcoder

try:
    
    while True:
        s = input().strip().split(',')
        s.sort()
        print(','.join(s))
except:
    pass

字符串排序3,使用模板

def getInput(choice='intType',sep=None):
	'''

	:param choice: ['intType', 'strType']
		intType, to get int data
		strType, to get str data
	:param sep:  split str.
	:return: a inpput list
	'''

	if choice =='intType':
		if sep:
			return list(map(int, input().strip().split(sep)))
		else:
			return list(map(int, input().strip().split()))
	elif choice =='strType':
		if sep:
			return list((input().strip().split(sep)))
		else:
			return list((input().strip().split()))
        
try:
    
    while True:
        # s = input().strip().split(',')
        s = getInput(choice ='strType', sep=',')
        s.sort()
        print(','.join(s))
except:
    pass

OJ在線編程常見輸入輸出練習場
https://ac.nowcoder.com/acm/contest/320#question

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章