PTA:7-3 银行业务队列简单模拟

p.s.自用。

题目

设某银行有A、B两个业务窗口,且处理业务的速度不一样,其中A窗口处理速度是B窗口的2倍 —— 即当A窗口每处理完2个顾客时,B窗口处理完1个顾客。给定到达银行的顾客序列,请按业务完成的顺序输出顾客序列。假定不考虑顾客先后到达的时间间隔,并且当不同窗口同时处理完2个顾客时,A窗口顾客优先输出。

输入格式

输入为一行正整数,其中第1个数字N(≤1000)为顾客总数,后面跟着N位顾客的编号。编号为奇数的顾客需要到A窗口办理业务,为偶数的顾客则去B窗口。数字间以空格分隔。

输出格式

按业务处理完成的顺序输出顾客的编号。数字间以空格分隔,但最后一个编号后不能有多余的空格。

输入样例

8 2 1 3 9 4 11 13 15

输出样例

1 3 2 9 11 4 13 15

代码长度限制:16 KB

时间限制:400 ms

内存限制:64 MB

栈限制:8192 KB

题目

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 循环队列
typedef struct queue {
	int* data;
	int head, tail;
	int maxNum;
}myQueue;

myQueue* init(int n) // 初始化
{
	myQueue* s = (myQueue*)malloc(sizeof(myQueue));
	s->data = (int*)malloc(sizeof(int) * (n + 1));
	s->head = s->tail = 0;
	s->maxNum = n + 1;
	return s;
}
int is(myQueue* s) // 判空:1
{
	if (s->head == s->tail)
		return 1;
	return 0;
}
void ppush(myQueue* s, int n) // 新增
{
	if ((s->tail + 1) % s->maxNum) // 判满
	{
		s->data[s->tail] = n;
		s->tail = (s->tail + 1) % s->maxNum;
	}
}
int find(myQueue* s) // 找到队首元素
{
	if (is(s)) // 判空
		return 0;
	return s->data[s->head];
}
void ppop(myQueue* s) // 删除
{
	if (is(s)) // 判空
		return;
	s->head = (s->head + 1) % s->maxNum;
}

int main()
{
	int n; // 顾客总数
	scanf("%d", &n);
	int* num = (int*)malloc(sizeof(int) * n); // 顾客编号
	myQueue* sa = init(n);
	myQueue* sb = init(n);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &num[i]);
		// 排队
		if (num[i] % 2 == 0) // 偶数-B
			ppush(sb, num[i]);
		else // 奇数-A
			ppush(sa, num[i]);
	}
	int count = 0;
	while (!is(sa) && !is(sb)) // 队列AB都不为空
	{
		int x = find(sa);
		ppop(sa);
		if (count != 0)
			printf(" ");
		if (x != 0)
		{
			count++;
			printf("%d", x);
		}
		else
			break;
		x = find(sa);
		ppop(sa);
		if (x != 0)
		{
			count++;
			printf(" %d", x);
		}
		else
			break; 
		x = find(sb);
		ppop(sb);
		if (x != 0)
		{
			count++;
			printf(" %d", x);
		}
		else
			break;
	}
	while (!is(sa)) // 队列A都不为空
	{
		int x = find(sa);
		ppop(sa);
		if (count != 0)
			printf(" ");
		count++;
		printf("%d", x);
	}
	while (!is(sb)) // 队列B都不为空
	{
		int x = find(sb);
		ppop(sb);
		if (count != 0)
			printf(" ");
		count++;
		printf("%d", x);
	}
}

### Python 银行业务队列模拟实现 以下是基于银行业务队列简单模拟示例代码,该程序通过 `queue.Queue` 来管理客户排队情况并处理业务请求。 #### 使用标准库中的 Queue 模块 Python 的 `Queue` 模块提供了线程安全的队列实现,适用于简单的银行队列场景。下面是一个完整的示例: ```python import queue import threading import time class BankSimulation: def __init__(self, num_tellers=2): self.customer_queue = queue.Queue() # 创建一个队列用于存储顾客 self.num_tellers = num_tellers # 告诉我们有多少个柜员 def add_customer(self, customer_id): """ 添加新顾客到队列 """ self.customer_queue.put(customer_id) def teller_job(self, teller_id): """ 定义柜员的工作逻辑 """ while True: try: customer_id = self.customer_queue.get(timeout=5) # 获取下一个顾客 print(f"柜员 {teller_id} 正在服务顾客 {customer_id}") time.sleep(2) # 模拟服务时间 print(f"柜员 {teller_id} 已完成服务顾客 {customer_id}") self.customer_queue.task_done() except queue.Empty: print(f"柜员 {teller_id} 发现没有更多顾客,准备休息...") break def main(): simulation = BankSimulation(num_tellers=3) # 设置有三个柜员 # 启动柜员线程 threads = [] for i in range(simulation.num_tellers): t = threading.Thread(target=simulation.teller_job, args=(i,)) threads.append(t) t.start() # 添加一些顾客进入队列 for j in range(10): # 总共有十个顾客 simulation.add_customer(j) # 等待所有顾客被服务完毕 simulation.customer_queue.join() if __name__ == "__main__": main() ``` 上述代码展示了如何利用多线程来模拟多个柜员同时工作的情况[^1]。每个柜员会从共享的队列中取出一位顾客进行服务,在一定时间内完成后继续取下一位直到队列为空为止。 #### 关键点解释 - **`queue.Queue()`**: 这是用来创建 FIFO (先进先出) 类型的数据结构实例。 - **`threading.Thread`**: 处理并发操作的核心组件之一;这里用来表示不同柜员可以独立运作而不互相干扰。 - **超时机制 (`timeout`) 和异常捕获**: 当前无可用资源时防止死锁发生,并允许系统优雅退出循环而非无限等待下去。 此方案不仅实现了基本功能需求还考虑到了实际应用环境下的鲁棒性和扩展性问题。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值