2024-11-01 08:18:50 +08:00

41 lines
1.3 KiB
Python

class 进程:
def __init__(self, 名称, 到达时间, 执行时间):
self.名称 = 名称
self.到达时间 = 到达时间
self.执行时间 = 执行时间
self.剩余时间 = 执行时间
self.顺序 = 0
def 最短剩余时间调度():
进程列表 = []
for i in range(4):
名称, 到达时间, 执行时间 = input().split()
进程列表.append(进程(名称, int(到达时间), int(执行时间)))
进程列表[-1].顺序 = i
当前时间 = 0
完成的进程 = []
结果 = []
while len(完成的进程) < 4:
可用进程 = [p for p in 进程列表 if p.到达时间 <= 当前时间 and p not in 完成的进程]
if not 可用进程:
下一个到达 = min([p.到达时间 for p in 进程列表 if p not in 完成的进程])
当前时间 = 下一个到达
continue
选择的进程 = min(可用进程, key=lambda x: (x.剩余时间, x.顺序))
if not 结果 or 结果[-1] != 选择的进程.名称:
结果.append(选择的进程.名称)
选择的进程.剩余时间 -= 1
当前时间 += 1
if 选择的进程.剩余时间 == 0:
完成的进程.append(选择的进程)
print(" ".join(结果))
最短剩余时间调度()