From 7d68c1e3236763711a47495abb4acf0d7c5c06fe Mon Sep 17 00:00:00 2001 From: GuanM <30427262+sxhoio@users.noreply.github.com> Date: Thu, 31 Oct 2024 17:41:33 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A2=98=E7=9B=AE2=2083%?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 计算机基础/题目2.py | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 计算机基础/题目2.py diff --git a/计算机基础/题目2.py b/计算机基础/题目2.py new file mode 100644 index 0000000..b4f8b7c --- /dev/null +++ b/计算机基础/题目2.py @@ -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(结果)) + + +最短作业优先调度() \ No newline at end of file