pta银行业务队列简单模拟C语言
时间: 2023-07-09 17:21:13 浏览: 165
好的,我可以帮你写一个pta银行业务队列的简单模拟程序,使用C语言实现。
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100 // 队列最大长度
// 定义队列结构体
typedef struct {
int data[MAX_SIZE]; // 存放队列元素的数组
int front; // 队头指针
int rear; // 队尾指针
} Queue;
// 初始化队列
void init(Queue *q) {
q->front = q->rear = 0;
}
// 判断队列是否为空
int is_empty(Queue *q) {
return q->front == q->rear;
}
// 判断队列是否已满
int is_full(Queue *q) {
return (q->rear + 1) % MAX_SIZE == q->front;
}
// 入队
void enqueue(Queue *q, int x) {
if (is_full(q)) {
printf("Queue is full.\n");
exit(1);
}
q->data[q->rear] = x;
q->rear = (q->rear + 1) % MAX_SIZE;
}
// 出队
int dequeue(Queue *q) {
if (is_empty(q)) {
printf("Queue is empty.\n");
exit(1);
}
int x = q->data[q->front];
q->front = (q->front + 1) % MAX_SIZE;
return x;
}
int main() {
Queue q;
init(&q);
int n, m;
scanf("%d %d", &n, &m);
int t, count = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &t);
enqueue(&q, t);
}
while (!is_empty(&q)) {
int x = dequeue(&q);
if (x > m) {
enqueue(&q, x - m);
count += m;
} else {
count += x;
}
}
printf("%d\n", count);
return 0;
}
```
以上就是一个简单的pta银行业务队列模拟程序,可以满足你的要求。
阅读全文
相关推荐
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.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/20241231045053.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)