博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
食堂排队问题的一个实现
阅读量:4478 次
发布时间:2019-06-08

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

问题场景:食堂,11点到1点,整分钟时学生进入食堂,最后的1点0秒不算在内,所以共120个时间点,进入食堂的学生数按高斯分布N(0,1/2PI),即y=e- x*x*PI,即x=0时 y=1,12点对应x=0,11点对应-0.5,1点对应0.49...,对高斯分布等比例放大N倍,即12点到达N人,现规定窗口数为K,所有窗口的服务时间满足[4,5,6]的随机分布,单位为秒,要求实现函数接口getAveTime(N,K)

我的实现,英文比较差,但又不想中文注释,偶尔会有乱码问题。

/*this is a piece of code about time ofline up  author: wzy  IDE: VC6*/#include
#include
#include
#include
#define PI 3.1415926#define FALSE 0#define TRUE 1struct Student{ int entry_time; int serve_time; struct Student *next;};typedef Student *Queueptr;struct LinkQueue{ Queueptr front; Queueptr rear; int last_leave_time;};void InitQueue(LinkQueue &Q);void DestroyQueue(LinkQueue &Q);void Enqueue(LinkQueue &Q, int start, int serve);bool Dequeue(LinkQueue &Q);bool IsEmpty(LinkQueue &Q);int getQueueLen(LinkQueue &Q);int findBestWin(LinkQueue win[], int len);int getServeTime();int getGaussianNum(int n, int time);int getTotalPeople(int n);float getAveTime(int n, int k);/* define totaltime as global variable*/int total_time = 0;int main(){ srand((int)time(0)); float aveTime = 0.0; int n = 1; int k = 3;/* printf("please input num of people:"); scanf("%d", &n); printf("\nplease input num of window:"); scanf("%d", &k);*/ aveTime = getAveTime(n , k); printf("num of people: %d\n", n); printf("num of window: %d\n", k); printf("total people: %d\n", getTotalPeople(n)); printf("total time: %d\n", total_time); printf("aveTime: %f", aveTime); return 0;}float getAveTime(int n, int k){ int people_index = 0; int total_people = 0; int win_index = 0; int win_num = k; int tick = 0; struct LinkQueue win[10]; total_people = getTotalPeople(n); /* do initialization for every window*/ for(win_index = 0; win_index < win_num; win_index++){ InitQueue(win[win_index]); } for(tick = 0; people_index < total_people ; tick++){ /*Enqueue when tick comes to minutes, tick(s) must less than 7200, when num of people equals to total people, loop ends. when enqueue, if the window is empty, we must reset the last_leave_time which means the time of the second student becomes the first, and when enqueue, we choose the shortest window, besides, we should set the entry_time and serve_time of students */ if(tick%60 == 0 && tick < 7200){ for(int j = 0; j < getGaussianNum(n, tick) ; j++){ int choose = findBestWin(win, win_num); if(getQueueLen(win[choose]) == 0) win[choose].last_leave_time = tick; Enqueue(win[choose], tick, getServeTime()); } //printf("\tenqueue people: %d\n", getGaussianNum(n, tick)); printf("tick:%d\n",tick); for(win_index = 0; win_index < win_num; win_index++){ printf("window%d num of people: %d\n", win_index, getQueueLen(win[win_index])); } } /*Dequeue when serve_time is satisfied, that is(last_leave_time - entry_time = serve_time) then we should add (current time(tick) - entry_time) to total_time as the wait_time, and add people plus 1, set last_leave_time equals to current time */ for(win_index = 0; win_index < win_num ; win_index++){ if(IsEmpty(win[win_index])){ continue; } //printf("window%d last_leave_time:%d firstman serve_time:%d\n", win_index, win[win_index].last_leave_time, win[win_index].front->next->serve_time); if((tick - win[win_index].last_leave_time) == win[win_index].front->next->serve_time){ //printf("window%d dequeue\n",win_index); total_time += (tick-win[win_index].front->next->entry_time); if(Dequeue(win[win_index])) people_index += 1; win[win_index].last_leave_time = tick; } } }/* not shure if we should free memory by self, when i do, error arise for(win_index = 0; win_index < win_num ; win_index++){ DestroyQueue(win[win_index]); }*/ return float(total_time)/float(total_people);}void InitQueue(LinkQueue &Q){ Q.front = Q.rear = new Student; Q.front->next = NULL; Q.last_leave_time = 0;}void DestroyQueue(LinkQueue &Q){ while(Q.front){ Q.rear = Q.front->next; delete Q.front; Q.front = Q.rear; }}void Enqueue(LinkQueue &Q, int start, int serve){ Student *s = new Student; s->entry_time = start; s->serve_time = serve; Q.rear->next = s; Q.rear = s;}bool Dequeue(LinkQueue &Q){ if(Q.front == Q.rear) return FALSE; Student *p = new Student; p = Q.front->next; Q.front->next = p->next; if(Q.rear == p) //only one node Q.rear = Q.front; delete p; //release memory return TRUE;}bool IsEmpty(LinkQueue &Q){ if(Q.front == Q.rear) return TRUE; else return FALSE;}int findBestWin(LinkQueue win[], int len){ if(len <= 0) printf("error: window num less than zero!!!\n"); int index = 0; int shortest_len = getQueueLen(win[0]); for(int i = 1; i < len; i++){ if(shortest_len == 0) return index; if(getQueueLen(win[i]) < shortest_len){ shortest_len = getQueueLen(win[i]); index = i; } } return index;}int getQueueLen(LinkQueue &Q){ if(Q.front == Q.rear) return 0; int len = 0; Student *p = new Student; p = Q.front; while(p != Q.rear){ p = p->next; len++; } return len;}int getServeTime(){ return rand()%3 + 4;}int getGaussianNum(int n, int time){ float x = float((time-3600))/7200; float num = n*exp(- x*x*PI); return int(num+0.5);}int getTotalPeople(int n){ int count = 0; for(int i = 0; i < 120; i++) count += getGaussianNum(n,i*60); return count;}

 

转载于:https://www.cnblogs.com/wzyuan/p/9768957.html

你可能感兴趣的文章
mysql中如何嵌套使用insert和select
查看>>
Linux内核socket优化项
查看>>
MongoDB的安装
查看>>
MySQL运行原理与基础架构
查看>>
.Net 从零开始构建一个框架之基本实体结构与基本仓储构建
查看>>
数据库之互联网常用分库分表方案
查看>>
改善过多的if else
查看>>
nginx启动、重启、关闭
查看>>
结构化方法与面向对象方法的比较
查看>>
简单的mongo小工具 python
查看>>
'Python.exe' 不是内部或外部命令,也不是可运行的程序 或批处理文件。
查看>>
jquery easyui tab加载内容的几种方法
查看>>
mysql数据库优化步骤
查看>>
Sublime Text 3 常用插件 —— SFTP
查看>>
python 终极篇 --- django 路由系统
查看>>
IntelliJ IDEA平台下JNI编程(五)—本地C代码创建Java对象及引用
查看>>
Android jni 二维数组 传递
查看>>
Swift - whose view is not in the window hierarchy 问题解决方法
查看>>
说一说ASCLL和Unicode
查看>>
Swift - 几种使用数组的数据存储模型
查看>>