银行业务的简单队列(2019-05-06)

在这里插入图片描述
以下是在网上搜寻的用数组编写的正确答案,把奇数的循环两次是因为A队为奇数。速度是B的两倍
#include<stdio.h>
#include<stdlib.h>
//输出
void Printf(int x);

int main()
{
int a[1001];
int n, i, j, k;
scanf("%d", &n);
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
j = 0;
k = 0;
while (j < n || k < n)
{
for (; j < n ; ++j)
{
if (a[j] % 2 == 1)
{
Printf(a[j++]);
break;
}
}
for (; j < n ; ++j)
{
if (a[j] % 2 == 1)
{
Printf(a[j++]);
break;
}
}
for (; k < n; ++k)
{
if (a[k] % 2 == 0)
{
Printf(a[k++]);
break;
}
}
}

 return 0;

}
//输出
void Printf(int x)
{
static int ok = 0;

if(ok)
{
	printf(" ");
}
else 
{
	ok = 1;
}

printf("%d", x);

}

以下为自己编写的队列的正确答案
#include<stdio.h>
#include<stdlib.h>

//定义结构体
typedef struct queue
{
int size[1000];//定义动态数组的尺寸
int front;//定义队首
int rear;//定义队尾
}queue;

//创建队列
void Createqueue(queue *q);
//判断队列是否为空
int Isqueue(queue *q);
//入队
void Addqueue(queue *q, int x);
//出队
int Deletequeue(queue *q);

int main()
{
queue A, B;
Createqueue(&A);
Createqueue(&B);
int n, i, x;
scanf("%d", &n);
//把编号分为奇数和偶数,分别入队
for (i = 0; i < n; ++i)
{
scanf("%d", &x);
if (x % 2 != 0)//若写作x != 1则else里面就不再是x % 2 == 0了
{
Addqueue(&A, x);
}
else
{
Addqueue(&B, x);
}
}
i = 0;
//判断循环的条件为看A,B队列是否为空
while (Isqueue(&A) || Isqueue(&B))
{
if (Isqueue(&A))
{
if (i == 0)
{
printf("%d", Deletequeue(&A));
i++;
}
else
{
printf(" %d", Deletequeue(&A));
}
}
//A队列速度是B队列的两倍
if (Isqueue(&A))
{
printf(" %d", Deletequeue(&A));
}
if (Isqueue(&B))
{
if(i == 0)
{
printf("%d", Deletequeue(&B));
i++;
}
else
{
printf(" %d", Deletequeue(&B));
}
}
}

return 0;

}

//创建队列
void Createqueue(queue *q)
{
q->rear = -1;//-1是结束标记
q->front = -1;
}
//判断队列是否为空
int Isqueue(queue *q)
{
int ok;
if (q->rear == q->front)
{
ok = 0;
}
else
{
ok = 1;
}

return ok;

}
//入队
void Addqueue(queue *q, int x)
{
q->rear++;
q->size[q->rear] = x;
}
//出队
int Deletequeue(queue *q)
{
q->front++;

return q->size[q->front];

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!感谢您的提问。要模拟银行队列多窗口的情况,可以使用数据结构中的双向队列(deque)来实现。双向队列在这里可以用来存储客户的信息以及排队顺序。以下是一个简单的示例代码: ```python from collections import deque class BankQueue: def __init__(self, num_windows): self.num_windows = num_windows self.queue = deque() def enqueue(self, customer): self.queue.append(customer) def dequeue(self): if not self.is_empty(): return self.queue.popleft() else: return None def is_empty(self): return len(self.queue) == 0 def get_queue_size(self): return len(self.queue) def get_num_windows(self): return self.num_windows class Customer: def __init__(self, id): self.id = id def get_id(self): return self.id # 创建银行队列 bank_queue = BankQueue(3) # 模拟客户入队 for i in range(1, 11): customer = Customer(i) bank_queue.enqueue(customer) # 模拟客户办理业务 while not bank_queue.is_empty(): for window in range(bank_queue.get_num_windows()): customer = bank_queue.dequeue() if customer: print(f"窗口{window+1}正在为客户{customer.get_id()}办理业务") else: break ``` 上述代码中,BankQueue类代表银行队列,通过双向队列实现了入队和出队操作。Customer类代表客户,每个客户有一个唯一的id。模拟时,先创建一个BankQueue对象,并指定窗口数量。然后依次将客户加入队列,最后通过循环模拟窗口依次为客户办理业务。 这只是一个简单的模拟示例,实际应用中可能需要更多的功能和细节处理。希望能对您有所帮助!如果您有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值