增补博客 第十七篇 python 模拟页面调度LRU算法

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
【题目描述】所谓LRU算法,是指在发生缺页并且没有空闲主存块时,把最近最少使用的页面换出主存块,腾出地方来调入新页面。<br>问题描述:一进程获得n个主存块的使用权,对于给定的进程访问页面次序,问当采用LRU算法时,输出发生的缺页次数。<br>【练习要求】请给出源代码程序和运行测试结果,源代码程序要求添加必要的注释。<br>【输入格式】在第一行中输入进程获得使用权的主存块数量n。<br>在第二行中输入进程访问页面的次序,各数据之间以空格为间隔。<br>【输出格式】输出对于给定的n和进程访问页面的次序,输出采用LRU算法时的缺页次数。<br>【输入样例】3<br>1 2 3 4 1 2 5 1 2 3 4 5<br>【输出样例】7<br><br>import math<br>
 
# 三维图形功能接口
class ThreeDimensionalShape:
    def perimeter(self):
        pass
 
    def area(self):
        pass
 
    def volume(self):
        pass
 
 
# 定义点类
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
 
    def get_coordinates(self):
        return self.x, self.y
 
    def set_coordinates(self, x, y):
        self.x = x
        self.y = y
 
    def display(self):
        print("点坐标: ({}, {})".format(self.x, self.y))
 
 
# 圆类
class Circle(Point):
    def __init__(self, x, y, radius):
        super().__init__(x, y)
        self.radius = radius
 
    def get_radius(self):
        return self.radius
 
    def set_radius(self, radius):
        self.radius = radius
 
    def perimeter(self):
        return 2 * math.pi * self.radius
 
    def area(self):
        return math.pi * self.radius ** 2
 
    def display(self):
        super().display()
        print("半径: ", self.radius)
        print("周长: ", self.perimeter())
        print("面积: ", self.area())
 
 
# 球类
class Sphere(Circle, ThreeDimensionalShape):
    def __init__(self, x, y, radius):
        super().__init__(x, y, radius)
 
    def volume(self):
        return (4 / 3) * math.pi * self.radius ** 3
 
    def display(self):
        super().display()
        print("体积: ", self.volume())
 
 
# 圆柱类
class Cylinder(Circle, ThreeDimensionalShape):
    def __init__(self, x, y, radius, height):
        super().__init__(x, y, radius)
        self.height = height
 
    def volume(self):
        return math.pi * self.radius ** 2 * self.height
 
    def display(self):
        super().display()
        print("高度: ", self.height)
        print("体积: ", self.volume())
 
 
# 圆锥类
class Cone(Circle, ThreeDimensionalShape):
    def __init__(self, x, y, radius, height):
        super().__init__(x, y, radius)
        self.height = height
 
    def volume(self):
        return (1 / 3) * math.pi * self.radius ** 2 * self.height
 
    def display(self):
        super().display()
        print("高度: ", self.height)
        print("体积: ", self.volume())
 
 
# 测试
point = Point(0, 0)
circle = Circle(1, 2, 5)
sphere = Sphere(3, 4, 6)
cylinder = Cylinder(5, 6, 3, 8)
cone = Cone(7, 8, 4, 10)
 
print("点:")
point.display()
print("-----------------")
print("圆:")
circle.display()
print("-----------------")
print("球:")
sphere.display()
print("-----------------")
print("圆柱:")
cylinder.display()
print("-----------------")
print("圆锥:")
cone.display()

  

posted @   财神给你送元宝  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示