洛谷模拟与高精度题单已经完成,对一些知识点和用法进行记录一下。以后能用到也可以参考。
这里的总结是对于之前写的博客中的题目,一些不熟练和新遇到,新学习的内容进行总结记录。
之前记录过结构体的使用。模拟题目之中大量用到结构体,这十分有利于一些多变量问题的处理。
(1)结构体单变量
class Person:
def __init__(self,n,c,m):
self.name=n
self.chinese=int(c)
self.math=int(m)
if __name__ == "__main__":
n= int(input())
people=[]
for item in range(n):
srt = input()
data = [str(x) for x in srt.split()]
people.append(Person(data[0], data[1], data[2]))
pass
for item in people:
print(item.name,item.chinese,item.math)
2
lihua 77 82
zhangsan 93 44
lihua 77 82
zhangsan 93 44
(2)结构体内数组
from functools import cmp_to_key
class Person:
def __init__(self,n,c,m):
self.name=n
self.chinese=int(c)
self.math=int(m)
self.list=[]
if __name__ == "__main__":
n= int(input())
people=[]
for item in range(n):
srt = input()
data = [str(x) for x in srt.split()]
people.append(Person(data[0], data[1], data[2]))
value_data=list(data[3])
for key in range(len(value_data)):
people[item].list.append(value_data[key])
pass
for item in people:
print(item.name,item.chinese,item.math)
print(item.list)
3
join 54 32 695432186
lisi 77 95 254136982
lihua 89 32 147852236
join 54 32
['6', '9', '5', '4', '3', '2', '1', '8', '6']
lisi 77 95
['2', '5', '4', '1', '3', '6', '9', '8', '2']
lihua 89 32
['1', '4', '7', '8', '5', '2', '2', '3', '6']
n=int(input())
mapp = [[jtem for jtem in range(item * n + 1, item * n + n + 1)] for item in range(n + 1)]
for item in range(n):
print(mapp[item])
9
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[10, 11, 12, 13, 14, 15, 16, 17, 18]
[19, 20, 21, 22, 23, 24, 25, 26, 27]
[28, 29, 30, 31, 32, 33, 34, 35, 36]
[37, 38, 39, 40, 41, 42, 43, 44, 45]
[46, 47, 48, 49, 50, 51, 52, 53, 54]
[55, 56, 57, 58, 59, 60, 61, 62, 63]
[64, 65, 66, 67, 68, 69, 70, 71, 72]
[73, 74, 75, 76, 77, 78, 79, 80, 81]
要对二维数组中子矩阵定位 单独操作就要对矩阵进行切片。这里用到的是numpy库中的操作
在这里插入代码片
import numpy as np
n=int(input())
mapp = [[jtem for jtem in range(item * n + 1, item * n + n + 1)] for item in range(n + 1)]
for item in range(n):
print(mapp[item])
mapp=np.array(mapp)
data=mapp[4:7,3:7]
print()
for item in range(3):
print(data[item])
9
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[10, 11, 12, 13, 14, 15, 16, 17, 18]
[19, 20, 21, 22, 23, 24, 25, 26, 27]
[28, 29, 30, 31, 32, 33, 34, 35, 36]
[37, 38, 39, 40, 41, 42, 43, 44, 45]
[46, 47, 48, 49, 50, 51, 52, 53, 54]
[55, 56, 57, 58, 59, 60, 61, 62, 63]
[64, 65, 66, 67, 68, 69, 70, 71, 72]
[73, 74, 75, 76, 77, 78, 79, 80, 81]
[40 41 42 43]
[49 50 51 52]
[58 59 60 61]
注意的是,切片定位后的数组是从0开始的,而不是之前数组的下标值。再就是n:m这样切片也是左闭右开的。右边的取不到,也就是说例子中4:7是4,5,6这三行。
通过上面的内容就可以对矩阵的子矩阵进行旋转:
import numpy as np
def work(flag,x_value,y_value,size):
global mapp
xx = x_value - size
yy = y_value - size
if flag==0:
key=np.rot90(mapp[xx:xx+2*size+1,yy:yy+2*size+1],3)
else:
key=np.rot90(mapp[xx:xx+2*size+1,yy:yy+2*size+1])
pass
mapp[xx:xx + 2 * size + 1, yy:yy + 2 * size + 1]=key
if __name__=="__main__":
n=int(input())
mapp = [[jtem for jtem in range(item * n + 1, item * n + n + 1)] for item in range(n + 1)]
mapp = np.array(mapp)
for item in range(n):
print(mapp[item])
work(0,3,4,3)
print()
for item in range(n):
print(mapp[item])
9
[1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18]
[19 20 21 22 23 24 25 26 27]
[28 29 30 31 32 33 34 35 36]
[37 38 39 40 41 42 43 44 45]
[46 47 48 49 50 51 52 53 54]
[55 56 57 58 59 60 61 62 63]
[64 65 66 67 68 69 70 71 72]
[73 74 75 76 77 78 79 80 81]
[ 1 56 47 38 29 20 11 2 9]
[10 57 48 39 30 21 12 3 18]
[19 58 49 40 31 22 13 4 27]
[28 59 50 41 32 23 14 5 36]
[37 60 51 42 33 24 15 6 45]
[46 61 52 43 34 25 16 7 54]
[55 62 53 44 35 26 17 8 63]
[64 65 66 67 68 69 70 71 72]
[73 74 75 76 77 78 79 80 81]
要对结构体 进行排序,那么就会牵扯到,要根据一个结构体中的不同变量进行排序。这样对于结构体中的sort()函数进行排序,就需要一个函数来告诉计算机要根据结构体的不同变量应该怎么排序,规则是如何的。就像之前在c++所运用的cmp函数一样,也是对结构体进行排序。
例如用上述初始化的结构体来作为例子:
(1)按照语文成绩从大到小进行排序
from functools import cmp_to_key
class Person:
def __init__(self,n,c,m):
self.name=n
self.chinese=int(c)
self.math=int(m)
def cmp(a,b):
if a.chinese<b.chinese:
return 1#进行交换
else:
return -1#不进行交换
if __name__ == "__main__":
n= int(input())
people=[]
for item in range(n):
srt = input()
data = [str(x) for x in srt.split()]
people.append(Person(data[0], data[1], data[2]))
pass
people.sort(key=cmp_to_key(cmp))
for item in people:
print(item.name,item.chinese,item.math)
5
join 32 54
kaiiti 84 33
lihua 25 36
zhangsan 98 87
lisi 72 36
zhangsan 98 87
kaiiti 84 33
lisi 72 36
join 32 54
lihua 25 36
这里要导入cmp函数,就要从functools库中导入cmp_to_key
进行交换就return 1.
(2)语文成绩相同时按照数学成绩进行排序:
from functools import cmp_to_key
class Person:
def __init__(self,n,c,m):
self.name=n
self.chinese=int(c)
self.math=int(m)
def cmp(a,b):
if a.chinese<b.chinese:
return 1
elif a.chinese>b.chinese:
return -1
else:
if a.math<b.math:
return 1
else:
return -1
if __name__ == "__main__":
n= int(input())
people=[]
for item in range(n):
srt = input()
data = [str(x) for x in srt.split()]
people.append(Person(data[0], data[1], data[2]))
pass
people.sort(key=cmp_to_key(cmp))
for item in people:
print(item.name,item.chinese,item.math)
8
student1 33 46
student2 88 72
student3 52 49
student4 88 58
student5 88 98
student6 88 32
student7 54 24
student8 96 97
student8 96 97
student5 88 98
student2 88 72
student4 88 58
student6 88 32
student7 54 24
student3 52 49
student1 33 46
python自带高精度,对于高精度的题目不必像c++那样运用字符模拟进行计算。模拟的题目应该就是带着一个繁琐的标签,在做题的时候应该把所有的需要条件梳理清楚。严格按照题目的描述进行做题。模拟的题目就是按照题目的描述一步一步的把步骤转换成代码。而高精度虽然py接受很大的数。但是也会有超出范围的时候。这需要引入sys库,使用sys.set_int_max_str_digits()函数进行扩展,py的默认的长度为4300.