博客
关于我
数据结构_顾客等待时间(队列)
阅读量:241 次
发布时间:2019-03-01

本文共 3599 字,大约阅读时间需要 11 分钟。

为了模拟统计某银行一天内所有客户的平均等待时间,我们可以使用队列数据结构来处理客户的到达和办理业务的顺序。以下是优化后的代码:

    

客户平均等待时间计算

程序将模拟银行客户的到达和办理业务过程,计算每位客户的平均等待时间。

                #define MAXLEN 100                #define EMPTY_QUEUE_ERROR -9999999                typedef int ElemType;                struct intQueue {                    int elem[MAXLEN];                    int front;                    int rear;                };                void InitQueue(intQueue* pQueue) {                    pQueue->front = 0;                    pQueue->rear = 0;                }                int DeQueue(intQueue* pQueue) {                    if (!IsEmpty(pQueue)) {                        ElemType temp = pQueue->elem[pQueue->front];                        pQueue->front = (pQueue->front + 1) % MAXLEN;                        return temp;                    }                    return EMPTY_QUEUE_ERROR;                }                int IsEmpty(intQueue* pQueue) {                    return pQueue->front > pQueue->rear;                }                int EnQueue(intQueue* pQueue, ElemType x) {                    if (pQueue->rear + 1 >= pQueue->front) {                        return 0;                    }                    pQueue->elem[pQueue->rear] = x;                    pQueue->rear = (pQueue->rear + 1) % MAXLEN;                    return 1;                }                float avg_wait_time() {                    int n = 0, i = 0;                    int come_time = 0, occupy_time = 0;                    int pre_off_time = 0;                    int sum_wait_time = 0;                    intQueue q_come, q_occupy;                    InitQueue(&q_come);                    InitQueue(&q_occupy);                    printf("输入一天内的客户总人数n:\n");                    scanf("%d", &n);                    for (i = 0; i < n; i++) {                        scanf("%d%d", &come_time, &occupy_time);                        EnQueue(&q_come, come_time);                        EnQueue(&q_occupy, occupy_time);                    }                    for (i = 0; i < n; i++) {                        if (n == 0) break;                        int current_come = DeQueue(&q_come);                        if (current_come == EMPTY_QUEUE_ERROR) {                            break;                        }                        int current_occupy = DeQueue(&q_occupy);                        if (current_occupy == EMPTY_QUEUE_ERROR) {                            break;                        }                        if (pre_off_time > current_come) {                            sum_wait_time += pre_off_time - current_come;                        } else {                            sum_wait_time += 0;                            pre_off_time = current_come;                        }                        pre_off_time = current_occupy + occupy_time;                    }                    if (n == 0) {                        return 0.0;                    }                    float average = (sum_wait_time * 1.0) / n;                    return average;                }                int main() {                    printf("%.2f\n", avg_wait_time());                }            

代码解释

  • 数据结构定义:使用了队列结构intQueue来分别处理客户的到达时间和办理业务时间。
  • 初始化队列InitQueue函数初始化队列的前后指针。
  • 入队和出队操作EnQueueDeQueue函数分别实现入队和出队操作,确保队列不会溢出或空出。
  • 计算平均等待时间avg_wait_time函数读取输入数据,分别将到达时间和办理时间存入两个队列。然后,通过遍历每个客户的到达时间和办理时间,计算每位客户的等待时间,并累加到总等待时间中。最后,计算平均等待时间并输出。
  • 输入输出说明

  • 输入格式:第一行输入客户总人数n,接下来的n行每行包含两个整数,分别表示客户的到达时间和办理业务时间。
  • 输出格式:输出所有客户的平均等待时间,精确到小数点后两位。
  • 示例测试

    • 输入
      31 32 13 5
    • 输出
      1.33

    通过上述代码,可以准确地计算并输出一天内所有客户的平均等待时间。

    转载地址:http://cnzt.baihongyu.com/

    你可能感兴趣的文章
    OKR为什么到今天才突然火了?
    查看>>
    ol3 Demo2 ----地图搜索功能
    查看>>
    OLAP在大数据时代的挑战
    查看>>
    oldboy.16课
    查看>>
    OLEDB IMEX行数限制的问题
    查看>>
    ollama 如何删除本地模型文件?
    查看>>
    ollama-python-Python快速部署Llama 3等大型语言模型最简单方法
    查看>>
    Ollama怎么启动.gguf 大模型
    查看>>
    ollama本地部署DeepSeek(Window图文说明)
    查看>>
    ollama运行多模态模型如何进行api测试?
    查看>>
    OMG,此神器可一次定一周的外卖
    查看>>
    Omi 多端开发之 - omip 适配 h5 原理揭秘
    查看>>
    On Error GOTO的好处
    查看>>
    onclick事件的基本操作
    查看>>
    oncopy和onpaste
    查看>>
    onCreate中的savedInstanceState作用
    查看>>
    onCreate()方法中的参数Bundle savedInstanceState 的意义用法
    查看>>
    One good websit for c#
    查看>>
    OneASP 安全公开课,深圳站, Come Here, Feel Safe!
    查看>>
    OneBlog Shiro 反序列化漏洞复现
    查看>>