题目2 83%
This commit is contained in:
parent
30252a773d
commit
7d68c1e323
52
计算机基础/题目2.py
Normal file
52
计算机基础/题目2.py
Normal file
@ -0,0 +1,52 @@
|
||||
class 进程:
|
||||
def __init__(self, 名称, 到达时间, 执行时间):
|
||||
self.名称 = 名称
|
||||
self.到达时间 = 到达时间
|
||||
self.执行时间 = 执行时间
|
||||
self.剩余时间 = 执行时间
|
||||
|
||||
|
||||
def 最短作业优先调度():
|
||||
进程列表 = []
|
||||
for _ in range(4):
|
||||
名称, 到达时间, 执行时间 = input().split()
|
||||
进程列表.append(进程(名称, int(到达时间), int(执行时间)))
|
||||
|
||||
当前时间 = 0
|
||||
完成数 = 0
|
||||
当前进程 = None
|
||||
执行序列 = []
|
||||
|
||||
while 完成数 < 4:
|
||||
可用进程 = [p for p in 进程列表
|
||||
if p.到达时间 <= 当前时间 and p.剩余时间 > 0]
|
||||
|
||||
if not 可用进程:
|
||||
下一个到达 = min([p.到达时间 for p in 进程列表
|
||||
if p.剩余时间 > 0])
|
||||
当前时间 = 下一个到达
|
||||
continue
|
||||
|
||||
最短作业 = min(可用进程,
|
||||
key=lambda x: (x.剩余时间, 进程列表.index(x)))
|
||||
|
||||
if 当前进程 is None or 当前进程 != 最短作业:
|
||||
当前进程 = 最短作业
|
||||
执行序列.append(最短作业.名称)
|
||||
|
||||
当前时间 += 1
|
||||
最短作业.剩余时间 -= 1
|
||||
|
||||
if 最短作业.剩余时间 == 0:
|
||||
完成数 += 1
|
||||
当前进程 = None
|
||||
|
||||
结果 = []
|
||||
for i in range(len(执行序列)):
|
||||
if i == 0 or 执行序列[i] != 执行序列[i - 1]:
|
||||
结果.append(执行序列[i])
|
||||
|
||||
print(" ".join(结果))
|
||||
|
||||
|
||||
最短作业优先调度()
|
Loading…
x
Reference in New Issue
Block a user