python3 ACM模式输入输出实例教学

  1. Python的输入是字符串,所以要自己转类型 
  2. strip去掉左右两端的空白符,返回str 
  3. slipt把字符串按空白符拆开,返回[str] 
  4. map把list里面的值映射到指定类型,返回[type] 
  5. EOF用抓异常 
  6. print后面加逗号就不会换行,否则反之,

1.例子

1.1 简单demo

题目:对10个正整数进行从小到大排序

a_1 = input() # 读取第二行
b_1 = [int(n) for n in a_1.split(' ')]  # 切割第一行
b_1.sort() 
for i in b_1 :
    print(i)  # 输出
Python代码简短,也更为人性化,让人摆脱繁琐的语言细节,平均代码量为C 的一半
极其方便的字符串,列表操作
Python具有函数式编程的特性
动态,交互式语言,带来DEBUG的便携


缺点也十分明显
损失了大量的时间和内存性能
因此,复杂的题可能会引起时间或内存超限
所以Python适合完成一些对性能要求不高的题

1.2 输入两行,不同的数

在新的一年,牛牛想跳得更高。牛牛本身可以跳高h米。同时牛牛有n颗跳高弹,使用第颗跳高弹可以使牛牛跳高高度增加a;米,且跳高弹的效果是可以叠加的,也就是说如果牛牛使用多颗跳高弹,那么他的跳高高度将会增加这些跳高弹单个效果的和。
每颗跳高弹只能使用一次。
请问牛牛最少需要使用多少个跳高弹,才能让牛牛的高度至少是u米高呢?数据保证答案存在。

 输入:

3 2 5

1 3 2

输出:

1

只需要使用第二颗跳高弹就可以达到5米

输入:

4 2 10

1 2 3 4

输出:

3

输入:
4 2 9
1 2 3 4


a_1=input() #读取第一行
a_2=input()  #读取第二行
b_1=[int(n) for n in a_1.split()]  #切割第一行
b_2=[int(n) for n in a_2.split()]
# 后面就是正常了,b_1[1]=10,  b_1[2]=9
print(b_2)
# ACM模式的代码
a_1 = input()
a_2 = input()
b_1 = [int(n) for n in a_1.split()]
b_2 = [int(n) for n in a_2.split()]
b_2 = sorted(b_2,reverse=True) #reverse -- 排序规则,reverse = True 降序 , reverse = False 升序(默认)
target = b_1[2] - b_1[1]
count = 0
sums = 0
for i in b_2:
    if sums<target:
        sums+=i
        count+=1
    else:
        break
print(count)

2.常见输入格式要求

2.1有多组输入数据,但没有具体的告诉你有多少组,只是让你对应每组输入,应该怎样输出?

多样例输入一组整数,每组数据占一行,每组数据中有两个数,要求输出两个数之和。每个结果占一行。

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

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

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

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

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

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

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

2.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))

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

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

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

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

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

2.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

2.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

3.python在acm中的一些知识

1.输入

a = input() #默认是字符
a, b, c = input()
a, b, c = map(int, input().split()) #在一行输入多个整型数
a, b, c = map(int, raw_input().split()) # python 2 中的输入
1
2
3
4
2.从句,循环

if i >= 0:

else :
for i in range(3): #注意冒号,相当于 i=0,i=1,i=2
for i in range(1,3): #i=1,i=2
while i >= 0:
1
2
3
4
5
6
3.运算符

and or not #没有 && || !,注意一下区别
/  #数学上的除
// #整除
1
2
3
4.注释

# 单行注释 pycharm中的快捷键 ctrl + /
''' 注释中间的部分 '''
1
2
5.列表(数组)

list = [] #定义
list.append(a) #在数组的末尾添加元素a
list.insert(i,a) #i为在哪一个位置插入a
list.extend([]) # 把某个列表插到list中,参数是一个列表
list.index(a) #在列表中搜索元素a,返回其位置
list.index(a,0,5) #在0,5搜索
list.remove(a) #删除第一个次出现的a
len(list) #列表长度
del list #把list在内存中清除
del list[1] #把list[1]删除
list[1:3] #只是拷贝 list[1],list[2] (3-1=2只有两个元素)
list1 = list[:] #列表拷贝
list2 = list #只是相等,list变,list2也变,只是指定另一个名字罢了
#列表之间的比较 默认从[0]开始
list = list1 + list2 # 合并列表
list * 3
# 列表判断元素
a in list 
a not in list
list.count(a) #a在list中出现的次数
list.reserve() #翻转
list.sort() 
list.sort(reserve = true) #从大到小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
6.格式化输出

print('%d %d %d'%(a, b, c))//这个很坑呀,(在一个括号内进行多个格式化输出)

物联沃分享整理
物联沃-IOTWORD物联网 » python3 ACM模式输入输出实例教学

发表评论