张芷铭的个人博客

ACM 模式需自行处理输入输出,掌握 input()split()map() 的组合用法是基础。

基本输入

1
2
3
4
# 输入: 1 2 3 4 5
a = input()           # '1 2 3 4 5' (字符串)
b = input().split()   # ['1', '2', '3', '4', '5'] (列表)
c = input().split(',')  # 逗号分隔

类型转换

1
2
3
4
5
6
7
8
# 单个数值
n = int(input())

# 批量转换 - 列表推导
nums = [int(i) for i in input().split()]

# 批量转换 - map
nums = list(map(int, input().split()))

常用模式

需求代码
读取整数int(input())
读取整数列表list(map(int, input().split()))
读取字符串列表input().split()
读取多行for _ in range(n): input()

map 返回值

map(int, input().split()) 返回迭代器,需用 list() 转换后才能索引或多次遍历。

Comments