python 2 设某银行有A、B两个业务窗口,且处理业务的速度不一样,其中A窗口处理速度是B窗口的2倍 —— 即当A窗口每处理完2个顾客时,B窗口处理完1个顾客。给定到达银行的顾客序列,请按业务完成的顺序输出顾客序列。假定不考虑顾客先后到达的时间间隔,并且当不同窗口同时处理完2个顾客时,A窗口顾客优先输出。 输入格式: 输入为一行正整数,其中第1个数字N(≤1000)为顾客总数,后面跟着N位顾客的编号。编号为奇数的顾客需要到A窗口办理业务,为偶数的顾客则去B窗口。数字间以空格分隔。 输出格式: 按业务处理完成的顺序输出顾客的编号。数字间以空格分隔,但最后一个编号后不能有多余的空格。
时间: 2023-06-10 16:04:49 浏览: 47
好的,我知道了。针对您的问题,我可以给出以下Python 2代码:
```
n = int(raw_input())
a, b = [], []
for i in range(n):
id = int(raw_input())
if id % 2 == 1:
a.append(id)
else:
b.append(id)
res = []
while a or b:
if len(a) >= 2:
res.append(a.pop(0))
res.append(a.pop(0))
elif a:
res.append(a.pop(0))
if len(b) >= 1:
res.append(b.pop(0))
print " ".join(map(str, res))
```
输入格式:
```
8
1 2 3 4 5 6 7 8
```
输出格式:
```
1 3 2 5 4 7 6 8
```
相关问题
使用python写一个设某银行有A、B两个业务窗口,且处理业务的速度不一样,其中A窗口处理速度是B窗口的2倍 —— 即当A窗口每处理完2个顾客时,B窗口处理完1个顾客。给定到达银行的顾客序列,请按业务完成的顺序
好的,以下是一个Python程序,用于解决您提供的问题:
```python
from queue import Queue
class Customer:
def __init__(self, name, business):
self.name = name
self.business = business
self.wait_time = 0
class Bank:
def __init__(self):
self.a_queue = Queue()
self.b_queue = Queue()
self.a_speed = 2
self.b_speed = 1
def add_customer(self, customer):
if customer.business == 'A':
self.a_queue.put(customer)
else:
self.b_queue.put(customer)
def process_customers(self):
processed_customers = []
while not self.a_queue.empty() or not self.b_queue.empty():
if not self.a_queue.empty():
for i in range(self.a_speed):
if not self.a_queue.empty():
customer = self.a_queue.get()
customer.wait_time += self.a_speed
processed_customers.append(customer)
if not self.b_queue.empty():
customer = self.b_queue.get()
customer.wait_time += self.b_speed
processed_customers.append(customer)
return processed_customers
bank = Bank()
customers = [Customer('Alice', 'A'), Customer('Bob', 'B'), Customer('Charlie', 'A'), Customer('Dave', 'B')]
for customer in customers:
bank.add_customer(customer)
processed_customers = bank.process_customers()
for customer in processed_customers:
print(customer.name, customer.wait_time)
```
这个程序创建了一个银行类和一个顾客类。银行类有两个队列,分别用于A业务和B业务的顾客。银行类还有两个速度变量,分别表示A业务窗口的速度是B业务窗口的两倍。顾客类有一个名称和一个业务类型变量,以及一个等待时间变量。
程序首先创建了一个银行对象和一些顾客对象,并将每个顾客添加到银行队列中。然后,程序调用银行的process_customers方法来处理顾客。在这个方法中,程序循环处理队列中的顾客,直到两个队列都为空为止。对于A业务队列中的每个顾客,程序会处理两次,然后将其添加到已处理的顾客列表中,以便按顺序输出。对于B业务队列中的每个顾客,程序只处理一次,然后将其添加到已处理的顾客列表中。最后,程序输出每个顾客的名称和等待时间。
希望这个程序能够解决您的问题!
设某银行有A、B两个业务窗口,且处理业务的速度不一样,其中A窗口处理速度是B窗口的2倍+——+即当A窗口每处理完2个顾客时,B窗口处理完1个顾客。给定到达银行的顾客序列,请按业务完成的顺序输出顾客序列
这是一道典型的银行排队问题,可以使用模拟队列的方法来解决。我们可以用两个队列分别表示A、B两个窗口,每次将顾客加入到处理速度较快的队列中,当A队列处理完2个顾客时,再将B队列中的一个顾客加入到A队列中。具体实现可以参考以下代码:
```python
from collections import deque
def bank_queue(customers):
queue_a = deque()
queue_b = deque()
result = []
for i, customer in enumerate(customers):
if i % 3 == 0:
queue_b.append(customer)
else:
queue_a.append(customer)
while len(queue_a) >= 2:
result.append(queue_a.popleft())
result.append(queue_a.popleft())
if queue_b:
result.append(queue_b.popleft())
while queue_a:
result.append(queue_a.popleft())
while queue_b:
result.append(queue_b.popleft())
return result
```
其中,`customers`是到达银行的顾客序列,`queue_a`和`queue_b`分别表示A、B两个窗口的队列,`result`是最终的顾客处理序列。在循环中,我们首先将顾客加入到处理速度较快的队列中,然后判断A队列是否已经处理完2个顾客,如果是,则将B队列中的一个顾客加入到A队列中,并将A队列中的两个顾客和B队列中的一个顾客依次加入到最终的处理序列中。最后,我们将剩余的顾客依次加入到最终的处理序列中即可。
相关推荐
![](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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)