//銀行排隊 #include<stdio.h> #include<stdlib.h> #include<time.h> #include<iostream> typedef struct nima{ int go; int type; }event; typedef struct hh{ event thing; int lenght; struct hh* next; }evlist; typedef struct oo{ int arrtiem; int durtime; }qelem; typedef struct kk{ qelem tt; struct kk*next; }data,*queptr; typedef struct { queptr front; queptr rear; int lenght; }linkque; void fistarry(linkque &fist ) { fist.front = fist.rear = (queptr)malloc(sizeof(data)); if(!fist.front)exit(0); fist.front->next = NULL; } void enarry(linkque &fist,int go,int dur) { data*p = (data*)malloc(sizeof(data)); p->tt.arrtiem = go; p->tt.durtime = dur; p->next = NULL; if(!p){ printf("error\n"); exit(0); } fist.rear->next = p; fist.rear = p; fist.lenght++; } int minimum(linkque fist[]) {int min =fist[0].lenght; int k; for(int i = 0;i<4;i++) { if(min>fist[i].lenght){ min = fist[i].lenght; k = i;} } return k; } void delarry(linkque& fist,qelem *cumter) {data*k; if(fist.lenght>0) {k=fist.front->next; *cumter =k->tt ; fist.front->next = k->next; if(fist.rear == k)fist.rear = fist.front; free(k); fist.lenght--; } } void getarry(linkque fist,qelem*cumter) { printf("%d\n",fist.lenght); *cumter = fist.front->next->tt; } //事件 void init(evlist**fist) { *fist =(evlist*)malloc(sizeof(evlist)); (*fist)->lenght = 0; (*fist)->next = NULL; } int diskong(evlist*fist) { if(fist->lenght == 0)return 1; else return 0; } int cmp(event i,event j) { int ii = i.go; int jj = j.go; if(ii>=jj)return 1; else return 0; } void insert(evlist* fist,event en,int(*cmpen)(event i,event j)) { int j,woca; woca = 0; evlist*p = (evlist*)malloc(sizeof(evlist)); p->thing = en; if(fist->next == NULL) { fist->next = p; p->next = NULL; } else{ evlist*key = fist; evlist*q = fist->next; for(;q;q= q->next) { j = (*cmpen)(p->thing,en); if(j == 1) { woca = 1; p->next = q; key->next = p;break; } key = q; } if(woca == 0){ key->next = p; p->next = NULL; } } fist->lenght++; } void sunji(int* dur,int *wait) { *dur = 10000; *wait = ; } evlist*shijian; event ee; qelem cc; int tot,ccnumber; linkque dui[4]; void openforday() { tot = 0;ccnumber = 0; init(&shijian); printf("dad\n"); ee.go = 0;ee.type = 0; insert(shijian,ee,cmp); for(int i= 0;i<4;i++) { fistarry(dui[i]); } } void custarr() { int i,j,t; event ll; event ll2; ccnumber++; sunji(&i,&j); t = ee.go+j; if(t<RAND_MAX%100000){ ll2.go = t; ll2.type = 0; insert(shijian,ll2,cmp); } int ii = minimum(dui); enarry(dui[ii],ee.go,i); if(dui[ii].lenght ==1){ ll.go = ee.go+i; ll.type = ii; insert(shijian,ll,cmp); printf("1 %d\n",shijian->lenght); } } void custdet() { int i = ee.type; event ll; delarry(dui[i],&cc); tot += ee.go-cc.arrtiem; if(dui[i].lenght!=0) { getarry(dui[i],&cc); ll.go = ee.go+cc.durtime; ll.type = i; insert(shijian,ll,cmp); printf("2 %d\n",shijian->lenght); } } void gethead(evlist*fist,event*en) { evlist* p; if(fist->next!=NULL) p = fist->next; *en = p->thing; fist->next = p->next; free(p); fist->lenght--; } int main() { srand(time(0)); openforday();printf("%d \n",shijian->lenght); while(!diskong(shijian)) {///printf("dadad"); gethead(shijian ,&ee); if(ee.type == 0) { custarr();//printf("ddd\n"); } else custdet(); printf("%d \n",shijian->lenght); printf("%d %d %d %d\n",dui[0].lenght,dui[1].lenght,dui[2].lenght,dui[3].lenght); } printf("%d %d %f\n",tot,ccnumber,(float)tot/ccnumber); return 0; }