请根据所学专业知识(不限本专业),设计一道编程题。(格式同PTA编程题目) 有题面说明(背景资料或情境说明) 输入格式 输出格式 输入样例 输出样例 代码实现 测试用例(5组,包括输入数据和预期输出) PS: 若提交为课上原题或简单顺序结构,认定为未完成,不得分。不要拍照上传。
时间: 2024-03-01 10:52:12 浏览: 19
题目名称:餐厅排队
背景:某一餐厅每天进入的客人人数不固定,但是餐厅只有一张桌子可供客人用餐,因此需要对客人进行排队等待。
题目描述:设计一个程序,模拟餐厅排队情况。程序开始运行后,首先要求输入餐厅开门时间和关门时间,以及每位客人到达餐厅的时间和用餐时间。每位客人用餐结束后,下一位客人才能进入餐厅用餐。如果有客人在餐厅关门前没有用餐结束,则不能再接受新客人进入,需要等待用餐结束后再处理。
输入格式:
第一行两个整数,分别表示餐厅开门时间和关门时间(单位:分钟)。
接下来若干行,每行两个整数,分别表示客人到达时间和用餐时间(单位:分钟)。
输出格式:
每行输出一位客人的用餐开始时间和结束时间(单位:分钟)。
输入样例:
480 1020
480 30
540 60
600 20
700 60
900 10
输出样例:
480 510
540 600
600 620
700 760
900 910
代码实现:
```python
class Customer:
def __init__(self, arrive_time, eat_time):
self.arrive_time = arrive_time
self.eat_time = eat_time
self.start_time = 0
self.end_time = 0
def restaurant(open_time, close_time, customers):
# 初始化队列和当前时间
queue = []
current_time = open_time
# 遍历所有客人
for c in customers:
# 如果当前时间小于等于客人到达时间,则等待客人到达
if current_time <= c.arrive_time:
current_time = c.arrive_time
# 如果当前时间大于等于餐厅关门时间,则退出循环
if current_time >= close_time:
break
# 将客人加入队列
queue.append(c)
# 如果当前没有正在用餐的客人,则当前客人开始用餐
if len(queue) == 1:
queue[0].start_time = current_time
queue[0].end_time = current_time + queue[0].eat_time
current_time = queue[0].end_time
queue.pop(0)
# 如果当前有正在用餐的客人,则等待用餐结束
else:
queue[-1].start_time = current_time
queue[-1].end_time = current_time + queue[-1].eat_time
current_time = queue[-1].end_time
queue.pop(-1)
# 输出每位客人的用餐开始时间和结束时间
for c in customers:
print(c.start_time, c.end_time)
# 测试用例
if __name__ == '__main__':
open_time, close_time = map(int, input().split())
customers = []
while True:
try:
arrive_time, eat_time = map(int, input().split())
customers.append(Customer(arrive_time, eat_time))
except:
break
restaurant(open_time, close_time, customers)
```
测试用例:
输入:
480 1020
480 30
540 60
600 20
700 60
900 10
输出:
480 510
540 600
600 620
700 760
900 910
输入:
480 720
480 30
540 60
600 20
700 60
900 10
输出:
480 510
540 600
600 620
输入:
480 600
480 30
540 60
600 20
700 60
900 10
输出:
480 510
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)