华为OD机考-构建输入和输出

华为OD机考-ACM模式下创建输入

概述:下文列举机考过程中常见输入的构造方法,如字符串、多维数组等,并对用到的函数进行详细讲解,最后通过真题实操巩固知识点。

一、机考中常见的输入

1. 输入字符串

# 输入一行字符串
str1 = input()
print(str1)
# 运行程序输入 hello ~566~
输出:hello ~566~

2. 输入一个或多个数字

方法1:分别输入整数a,b
# 输入整数a 
# 输入整数b,求a+b
# 方法1:先输入a,在输入b
a = int(input())
b = int(input())
print(a + b)
C:\Users\JUNE\AppData\Local\Continuum\anaconda3\python.exe C:/python基础语法练习/csdn.py
4
3
7
Process finished with exit code 0
方法2:使用MAP函数在一行同时输入a,b,以空格分开
#方法2:输入a b(在一行)
a,b = map(int,input().split())
print(a + b)
C:\Users\JUNE\AppData\Local\Continuum\anaconda3\python.exe C:/python基础语法练习/csdn.py
4 6
10
Process finished with exit code 0

3. 输入多个数据(一维数组)

博主对列表操作比较熟练,因此会在输入过程中将输入转换为列表

类型1:有限制输入数据数量
n = int(input())
lst = []
for i in range(n):
	lst.append(int(input()))
print(n)
print(lst)
C:\Users\JUNE\AppData\Local\Continuum\anaconda3\python.exe C:/python基础语法练习/csdn.py
5
1
2
3
4
5
5
[1, 2, 3, 4, 5]
Process finished with exit code 0
类型2:无数量限制且输入以空格分隔
# 方法2:无数量限制,且输入以空格分割时
lst = list(map(int,input().split()))
print(lst)
C:\Users\JUNE\AppData\Local\Continuum\anaconda3\python.exe C:/python基础语法练习/csdn.py
1 2 3 4 5
[1, 2, 3, 4, 5]
Process finished with exit code 0

4. 构建二维矩阵

类型1:创建m*n的矩阵,矩阵中的值为#
m,n = map(int(input().split()))
dp = [["#"] * n for _ in range(m)]
print(dp)
C:\Users\JUNE\AppData\Local\Continuum\anaconda3\python.exe C:/python基础语法练习/csdn.py
5 6
[['#', '#', '#', '#', '#', '#'], ['#', '#', '#', '#', '#', '#'], ['#', '#', '#', '#', '#', '#'], ['#', '#', '#', '#', '#', '#'], ['#', '#', '#', '#', '#', '#']]

Process finished with exit code 0
类型2:创建m*n的矩阵,矩阵中的值为随机输入
m,n = map(int,input().split())
dp = []
for i in range(m):
	dp.append([int(x) for x in input().split()])
print(dp)
C:\Users\JUNE\AppData\Local\Continuum\anaconda3\python.exe C:/python基础语法练习/csdn.py
5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
[[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]]
Process finished with exit code 0
类型3:输入蛇形矩阵(行数确定,列输入数量不确定)
m = int(input())
dp = []
for i in range(m):
	dp.append(list(map(int,input().split())))
print(dp)
C:\Users\JUNE\AppData\Local\Continuum\anaconda3\python.exe C:/python基础语法练习/csdn.py
5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
[[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5]]
Process finished with exit code 0

二、函数说明

1. input()

文字讲解

函数input()让程序暂停运行,等待用户输入一些文本。获取用户输入后,Python将其存储在一个变量中
变量类型默认为字符串
建议:使用函数时,指定清晰而易于明白的提示**(在考试中不用输入提示)**
引用自《python编程从入门到实践》Eric Matthes著

示例
name = input("Please enter your name: ")
print("Hello, " + name +)
C:\Users\JUNE\AppData\Local\Continuum\anaconda3\python.exe C:/python基础语法练习/csdn.py
Please enter your name: ~566~
Hello, ~566~!

Process finished with exit code 0

2. list()

(1)作为python的内置函数,可以将任何可迭代数据转化为列表类型,并返回转化后的列表。列表符号[]
(2) 列表常见的操作:排序,删除元素,插入元素,修改元素,遍历列表,切片等
(3)列表的索引从0开始

语法示例
#语法
list(object)  # object带转化为列表的数据类型
# 将student = ("xiaoming","zhangsan","lisi")
student = ("xiaoming","zhangsan","lisi")
print(list(student))
C:\Users\JUNE\AppData\Local\Continuum\anaconda3\python.exe C:/python基础语法练习/csdn.py
['xiaoming', 'zhangsan', 'lisi']
Process finished with exit code 0

3. split()

指定分隔符对字符串进行切片,如果参数num有指定值,则分割num+1个字符串

语法示例
# 语法
str.split(str = "",num = string.count(str))
# 示例
str1 = input()
result = str1.split("a",3)
print(result)
C:\Users\JUNE\AppData\Local\Continuum\anaconda3\python.exe C:/python基础语法练习/csdn.py
abcdfagedary
['', 'bcdf', 'ged', 'ry']
Process finished with exit code 0
输入示例
lst = list(map(int,input().split(",")))
print(lst)
C:\Users\JUNE\AppData\Local\Continuum\anaconda3\python.exe C:/python基础语法练习/csdn.py
1,2,3,4,5,6
[1, 2, 3, 4, 5, 6]
Process finished with exit code 0

4. strip()

移除字符串头尾指定的字符(默认为空格或换行)或字符序列

语法示例
#语法
str.strip([chars])  # char移除字符串头尾指定序列
# 示例
str1 = " qwertyuiop "
result = str1.strip(" ")
print(result)
C:\Users\JUNE\AppData\Local\Continuum\anaconda3\python.exe C:/python基础语法练习/csdn.py
qwertyuiop
Process finished with exit code 0

5. map()

根据提供的函数对指定序列做映射
第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。
python3.X返回迭代器

语法示例
# 语法
map(function,iterable,...)  # function 函数,iterable 一个或多个序列。

>>> def square(x) :         # 计算平方数
...     return x ** 2
...
>>> map(square, [1,2,3,4,5])    # 计算列表各个元素的平方
<map object at 0x100d3d550>     # 返回迭代器
>>> list(map(square, [1,2,3,4,5]))   # 使用 list() 转换为列表
[1, 4, 9, 16, 25]
>>> list(map(lambda x: x ** 2, [1, 2, 3, 4, 5]))   # 使用 lambda 匿名函数
[1, 4, 9, 16, 25]

本案例引用https://www.runoob.com/python/python-func-map.html

三、示例

给定一个非空字符串S,其被N个‘-’分隔成N+1的子串,给定正整数K,要求除第一个子串外,其余的子串每K个字符组成新的子串,并用‘-’分隔。对于新组成的每一个子串,如果它含有的小写字母比大写字母多,则将这个子串的所有大写字母转换为小写字母;反之,如果它含有的大写字母比小写字母多,则将这个子串的所有小写字母转换为大写字母;大小写字母的数量相等时,不做转换。
输入描述:
输入为两行,第一行为参数K,第二行为字符串S。
输出描述:
输出转换后的字符串。
示例1
输入
3
12abc-abCABc-4aB@
输出
12abc-abc-ABC-4aB-@
作者:yaozi
链接:https://leetcode.cn/circle/discuss/niKSMZ/

k = int(input())   #输入整数K,按照K分隔 3
str1 = input()     # 输入字符串12abc-abCABc-4aB@
lst = str1.split("-")  # 将字符串按照“-”分割
#print(lst)       # 输出:['12abc', 'abCABc', '4aB@']
result = []     # 定义一个空列表
result.append(lst[0]) # 第一个字符串不变,先加到result中
for i in lst[1:]: #第一次找到了abCABc
    for j in range(0,len(i),k):  # 遍历i
        uppers = []   # 大写
        lowers = []   # 小写
        for m in i[j:j+k]:   # 截取abCABc,间隔k
            if m.isupper():
                uppers.append(m) # 判断是否大写
            if m.islower():   # 判断是否小写
                lowers.append(m)
        if len(uppers) > len(lowers):  # 判断小写字符多还是大写字符多
            result.append(i[j:j+k].upper())
        elif len(uppers) < len(lowers):
            result.append(i[j:j + k].lower())
        else:
            result.append(i[j:j + k])
print("-".join(result))    # 用-拼接字符串
C:\Users\JUNE\AppData\Local\Continuum\anaconda3\python.exe C:/python基础语法练习/1.字符串分割.py
3
12abc-abCABc-4aB@
12abc-abc-ABC-4aB-@

Process finished with exit code 0

你可能感兴趣的:(python,开发语言)