Python3下ACM模式的输入输出牛客例子

1、多组输入,但是没说多少组。

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

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

tcase=int(input().strip())
for case in range(tcase):
    a,b=map(int,input().strip().split())
    print(a+b)

3、多组数据,并告知何时结束输入,例如(0 0)

while True:
    a,b=map(int,input().strip().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、多组数据,但是不知道多少组。每一行第一个数字代表这一组共有几个数据。

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

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()
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

  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值