zl程序教程

您现在的位置是:首页 >  Python

当前栏目

python3 牛客网:OJ在线编程常见输入输出练习(ACM模式)

2023-02-18 16:37:15 时间

 牛客网: 校招笔试真题_C++工程师、golang工程师_牛客网

其他语言输入输出见链接

1.输入两个数,输入数据包括多组。

while True:
    try:
        a = list(map(int,input().split()))
        print(a[0]+a[1])
    except:
        break
while True:
    try:
        a,b=map(int,input().strip().split())
        print(a+b)
    except EOFError:
        break

2.第一行为一个整数,告诉你有几组数据,剩下的行是每组的数据

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

 3.输入数据有多组, 如果输入为0 0则结束输入

while True:
    a, b = map(int, input().split())
    if a == 0 and b == 0:
        break
    print(a + b)

 4.多组数据,每一行第一个数字代表这一组共有几个数据。当行中第一个数字为0时结束。

while True:
    data=list(map(int,input().strip().split()))
    n,array=data[0],data[1:]
    if n==0:
        break
    print(sum(array))

 5.第一行的整数表示一共几组数据,剩下的行每一行第一个数字代表这一组共有几个数据

tcase=int(input().strip())
for case in range(tcase):
    data=list(map(int,input().strip().split()))
    n,array=data[0],data[1:]
    print(sum(array))

 6.多组数据,但是不知道多少组。每一行第一个数字代表这一组共有几个数据。

 7.多组数据,但是不知道有多少组,每一组的数据个数也不一样。

while True:
    try:
        data=list(map(int,input().strip().split()))
        print(sum(data))
    except EOFError:
        break

 8. 输入有两行,第一行n 。第二行是n个空格隔开的字符串,进行排序

n=int(input())
words=[x for x in input().strip().split()]
words.sort() #list.sort( key=None, reverse=False)
#reverse -- 排序规则,reverse = True 降序, reverse = False 升序(默认)。
for word in words:
    print(word,end=' ')
print()

 9 .多个组,每个测试组是一行,字符串通过空格隔开。

while True:
    try:
        words=[x for x in input().strip().split()]
        words.sort()
        for word in words:
            print(word,end=' ')
        print()
    except EOFError:
        break

 10.多个组,每个测试组是一行,字符串通过空格隔开。输出:每个字符串间用','隔开,无结尾空格。

while True:
    try:
        words=[x for x in input().strip().split(',')]
        words.sort()
        for word in words[:-1]:
            print(word,end=',')
        print(words[-1])
    except EOFError:
        break