主頁 > 資料庫 > 銀行排隊系統

銀行排隊系統

2020-09-20 21:45:52 資料庫

問題描述

?假設銀行有K個視窗提供服務,從早晨銀行開門(開門9:00am,關門5:00pm)起不斷有客戶進入銀行。由于每個視窗在某個時刻只能接待一個客戶,對于剛進入銀行的客戶(建議:客戶進入時間使用隨機函式產生),首先前往取號機取服務序號,取號機會自動列印出一張服務單。單上顯示服務號及該服務號前面正在等待的人數。當銀行職員服務完一個客戶后,只需按呼叫相應鍵,即可自動呼叫下一位客戶。

基本要求

(1)編制一個程式模擬銀行這種業務活動。
(2)計算一天中N個客戶在銀行的平均等待時間,最長等待時間,最后完成時間,并且統計每個視窗服務了多少顧客。
(3)建議有如下設定:
a)客戶到達的時間隨機,一天的人數設定為N人。
b)銀行業務員處理時間隨機產生。

代碼實作

#include<iostream>
 #include <stdlib.h>
 #include <time.h> 
#include<windows.h> 
using namespace std; int K,N; //定義銀行服務視窗個數和一天的客戶人數 //歡迎界面函式 void welcom(){ cout<<"*****************************歡迎光臨山大銀行****************************"<<endl; cout<<"銀行的營業時間為 9:00 -17:00,請您注意安排自己的時間,歡迎您的再次到來!"<<endl; } //定義鏈表節點 template<class T> struct chainNode {
 //資料成員 T element;
 chainNode<T> *next;
 //方法 chainNode(){}
 chainNode(const T& element){ this->element=element; }
 chainNode(const T& element,chainNode<T> *next){ this->element=element; this->next=next; } }; //定義佇列 template<class T> class queue{ public: queue(); //初始化佇列 ~queue(){}; //解構式 bool empty(){ //判斷佇列是否為空 return queueSize==0; } int size(){ //回傳佇列中元素的個數 return queueSize; } T& front(){ //回傳佇列首元素 return queueFront->element; } T& back(){ //回傳佇列尾元素 return queueBack->element; } chainNode<T> * begin(){ return queueFront; } void pop(); //洗掉首元素 void push(const T& theElement); private: int queueSize; //佇列長度 chainNode<T> *queueFront; //指向佇列第一個元素的指標 chainNode<T> *queueBack; //指向佇列最后一個元素的指標 }; template<class T> queue<T>::queue(){ queueFront=queueBack=NULL; queueSize=0; } template<class T> void queue<T>::pop(){ if(queueFront==NULL) { cout<<"you queue is empty!"; return; } chainNode<T> *nextNode=queueFront->next; delete queueFront; queueFront=nextNode; queueSize--; } template<class T> void queue<T>::push(const T& theElement){ //申請新元素節點 chainNode<T> *newNode=new chainNode<T>(theElement,NULL); //把新節點插入隊尾 if(queueSize==0) queueFront=newNode; //隊空 else queueBack->next=newNode; //隊非空 queueBack=newNode; queueSize++; } //定義時間 struct thetime{ int h; //時 int m; //分 }; //定義每個客戶的到達銀行時間,等待時間,開始辦理業務時間,業務處理時間,結束業務時間(離開銀行時間) struct customers{ struct thetime arrive_time; //到達銀行時間 struct thetime wait_time; //等待時間 struct thetime start_time; //開始辦理業務時間 int business_time; //業務處理時間(假設業務處理時間為整數,且處理時間范圍為10-40min) struct thetime end_time; //結束辦理業務時間 int in_bank_number; //客戶進入銀行的序號 }; //展示視窗服務情況 void show_queue(queue<customers> cus_queue[]){ Sleep(1000); for(int i = 0;i < K;i++) { cout<<"視窗"<<i+1<<": "; chainNode<customers> *m = cus_queue[i].begin(); while(m != NULL) { cout<<"客戶"<<m->element.in_bank_number<<" "; m = m -> next; } cout<<endl; } cout<<endl; } //用戶到達銀行時間表函式 void customers_time(struct customers &c){ //隨機產生客戶到達銀行時間 c.arrive_time.h=9+rand()%8; c.arrive_time.m=rand()%60; //隨機產生客戶業務處理時間 //c.business_time=10+rand()%31; } //按用戶的到達時間將用戶的先后順序進行排序(由于用戶的到達時間是隨機產生的) void customer_sort(customers customer[]){ int max_time_index; //記錄客戶到達的最晚時間對應的下標 customers max_time_cus,swap_cus; //采用選擇排序進行排序 for(int i=N-1;i>0;i--) { max_time_cus=customer[i]; max_time_index=i; //找出到達時間最晚的 for(int j=0;j<i;j++) { if((customer[j].arrive_time.h)*60+customer[j].arrive_time.m > (max_time_cus.arrive_time.h)*60+max_time_cus.arrive_time.m) { max_time_cus=customer[j]; max_time_index=j; } } if(i!=max_time_index){ //swap部分 swap_cus=customer[i]; customer[i]=max_time_cus; customer[max_time_index]=swap_cus; } } } //判斷客戶需要去哪個視窗排隊,即算出等待時間最少的佇列 int judge_queue_in(queue<customers> cus_queue[],customers &customer,int each_queue_cus_number[]) { //將用戶在每個佇列需要等待的時間放在一個陣列里 int each_queue_wait_time[K]; for(int i=0;i<K;i++) { //客戶的等待時間取決于他隊伍最后一個人的結束辦理時間 int wait_h=cus_queue[i].back().end_time.h-customer.arrive_time.h; each_queue_wait_time[i]=wait_h*60+cus_queue[i].back().end_time.m-customer.arrive_time.m; } //找出需要時間最少的佇列 int min_time_queue_index=0; for(int j=1;j<K;j++) { if(each_queue_wait_time[j] < each_queue_wait_time[min_time_queue_index]) min_time_queue_index=j; } //定義客戶的各項資料 customer.business_time=10+rand()%31; customer.wait_time.h=each_queue_wait_time[min_time_queue_index]/60; customer.wait_time.m=each_queue_wait_time[min_time_queue_index]%60; customer.start_time.h=cus_queue[min_time_queue_index].back().end_time.h; customer.start_time.m=cus_queue[min_time_queue_index].back().end_time.m; customer.end_time.h=customer.start_time.h+(customer.start_time.m+customer.business_time)/60; customer.end_time.m=(customer.start_time.m+customer.business_time)%60; //將客戶加入佇列 //對銀行關門時間到了,還在辦理的客戶允許繼續辦理完業務再離開,還在排隊的進馬上離開 if((customer.start_time.h)*60+customer.start_time.m < 17*60) { cus_queue[min_time_queue_index].push(customer); each_queue_cus_number[min_time_queue_index]++; } return min_time_queue_index; } //判斷下一個客戶到來時哪個佇列的隊首客戶是否已經辦理完業務,并進行出隊 void leave_queue(queue<customers> cus_queue[],customers customer){ for(int i=0;i<K;i++) { while(!cus_queue[i].empty() && (cus_queue[i].front().start_time.h)*60+cus_queue[i].front().start_time.m+ cus_queue[i].front().business_time <= (customer.arrive_time.h)*60+customer.arrive_time.m) { cout<<"----------客戶"<<cus_queue[i].front().in_bank_number<<"離開了視窗"<<i+1<<"----------"<<endl; cus_queue[i].pop(); } } //show_queue(cus_queue); } //用戶進入佇列函式 void customers_in_queue(queue<customers> cus_queue[],customers customer[],int each_queue_cus_number[]) { //判斷哪個視窗是否有空閑的 int queue_number; for(int i=0;i<N;i++) { bool queue_free=false; //每次進入佇列判斷各隊首是否已經辦理完業務 leave_queue(cus_queue,customer[i]); for(int j=0;j<K;j++) { //視窗中有空閑的情況 if(cus_queue[j].empty()) { //客戶每進入一個佇列都需要判斷每個隊首客戶是否已經辦理完業務 customer[i].business_time=10+rand()%31; customer[i].wait_time.h=0; customer[i].wait_time.m=0; customer[i].start_time.h=customer[i].arrive_time.h; customer[i].start_time.m=customer[i].arrive_time.m; customer[i].end_time.h=customer[i].start_time.h+(customer[i].start_time.m+customer[i].business_time)/60; customer[i].end_time.m=(customer[i].start_time.m+customer[i].business_time)%60; cus_queue[j].push(customer[i]); each_queue_cus_number[j]++; queue_free=true; cout<<"----------客戶"<<customer[i].in_bank_number<<"到達了視窗"<<j+1<<"----------"<<endl; cout<<"客戶"<<customer[i].in_bank_number<<"需等人數:0"<<endl; break; } } //視窗中沒有空閑的情況 if(queue_free==false){ queue_number = judge_queue_in(cus_queue,customer[i],each_queue_cus_number); //判斷哪個佇列的等待時間最少 cout<<"----------客戶"<<customer[i].in_bank_number<<"到達了視窗"<<queue_number<<"----------"<<endl; cout<<"客戶"<<customer[i].in_bank_number<<"需等人數:"<<cus_queue[queue_number].size()-1<<endl; } show_queue(cus_queue); } //展示最后客戶的離開狀態 leave_queue(cus_queue,customer[N]); show_queue(cus_queue); } int main(){ //srand(time(0)); srand((unsigned int)time(NULL)); //使每次編譯完后產生的亂數不同 welcom(); //歡迎界面 cout<<"請輸入銀行服務視窗的個數:"; cin>>K; cout<<"請輸入銀行一天的客戶人數:"; cin>>N; customers customer[N]; queue<customers> cus_queue[K]; int each_queue_cus_number[N]; for(int i=0;i<N;i++){ customers_time(customer[i]); //初始用戶時間 each_queue_cus_number[i]=0; //初始視窗服務客戶個數 cout<<i+1<<" arrive_time: "<<customer[i].arrive_time.h<<": "; cout<<customer[i].arrive_time.m<<endl; } customer_sort(customer); //按客戶進入銀行的先后順序進行排序 cout<<"---------------------------after sort---------------------------"<<endl; for(int i=0;i<N;i++){ //Sleep(2000); customer[i].in_bank_number = i + 1; cout<<i+1<<" arrive_time: "<<customer[i].arrive_time.h<<": "; cout<<customer[i].arrive_time.m<<endl; } cout<<"---------------------------begin serve---------------------------"<<endl; customers_in_queue(cus_queue,customer,each_queue_cus_number); //客戶進佇列 cout<<"---------------------------end serve---------------------------"<<endl; cout<<"---------------------------after customer in queue---------------------------"<<endl; for(int i=0;i<N;i++){ cout<<i+1<<" start_time: "<<customer[i].start_time.h<<":"<<customer[i].start_time.m<<"\t"; cout<<i+1<<" end_time: "<<customer[i].end_time.h<<":"<<customer[i].end_time.m<<"\t"; cout<<i+1<<" bussiness_time: "<<customer[i].business_time<<"min\t"; cout<<i+1<<" wait_time: "<<(customer[i].wait_time.h)*60+customer[i].wait_time.m<<" "<<endl; } int max_cus_wait_time=(customer[0].wait_time.h)*60+customer[0].wait_time.m; int sum_cus_wait_time=max_cus_wait_time; for(int i=1;i<N;i++){ if((customer[i].wait_time.h)*60+customer[i].wait_time.m > max_cus_wait_time) max_cus_wait_time=(customer[i].wait_time.h)*60+customer[i].wait_time.m; sum_cus_wait_time+=(customer[i].wait_time.h)*60+customer[i].wait_time.m; } int actual_cus_numbers=0; for(int i=0;i<K;i++){ cout<<"視窗"<<i+1<<"服務顧客個數: "<<each_queue_cus_number[i]<<endl; actual_cus_numbers+=each_queue_cus_number[i]; } cout<<"max_cus_wait_time: "<<max_cus_wait_time<<"min"<<endl; cout<<"avg_cus_wait_time: "<<(double)sum_cus_wait_time/actual_cus_numbers<<"min"<<endl; return 0; } 

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/91929.html

標籤:基礎類

上一篇:有沒有大佬會寫棋牌游戲的?

下一篇:Python問題

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • GPU虛擬機創建時間深度優化

    **?桔妹導讀:**GPU虛擬機實體創建速度慢是公有云面臨的普遍問題,由于通常情況下創建虛擬機屬于低頻操作而未引起業界的重視,實際生產中還是存在對GPU實體創建時間有苛刻要求的業務場景。本文將介紹滴滴云在解決該問題時的思路、方法、并展示最終的優化成果。 從公有云服務商那里購買過虛擬主機的資深用戶,一 ......

    uj5u.com 2020-09-10 06:09:13 more
  • 可編程網卡芯片在滴滴云網路的應用實踐

    **?桔妹導讀:**隨著云規模不斷擴大以及業務層面對延遲、帶寬的要求越來越高,采用DPDK 加速網路報文處理的方式在橫向縱向擴展都出現了局限性。可編程芯片成為業界熱點。本文主要講述了可編程網卡芯片在滴滴云網路中的應用實踐,遇到的問題、帶來的收益以及開源社區貢獻。 #1. 資料中心面臨的問題 隨著滴滴 ......

    uj5u.com 2020-09-10 06:10:21 more
  • 滴滴資料通道服務演進之路

    **?桔妹導讀:**滴滴資料通道引擎承載著全公司的資料同步,為下游實時和離線場景提供了必不可少的源資料。隨著任務量的不斷增加,資料通道的整體架構也隨之發生改變。本文介紹了滴滴資料通道的發展歷程,遇到的問題以及今后的規劃。 #1. 背景 資料,對于任何一家互聯網公司來說都是非常重要的資產,公司的大資料 ......

    uj5u.com 2020-09-10 06:11:05 more
  • 滴滴AI Labs斬獲國際機器翻譯大賽中譯英方向世界第三

    **桔妹導讀:**深耕人工智能領域,致力于探索AI讓出行更美好的滴滴AI Labs再次斬獲國際大獎,這次獲獎的專案是什么呢?一起來看看詳細報道吧! 近日,由國際計算語言學協會ACL(The Association for Computational Linguistics)舉辦的世界最具影響力的機器 ......

    uj5u.com 2020-09-10 06:11:29 more
  • MPP (Massively Parallel Processing)大規模并行處理

    1、什么是mpp? MPP (Massively Parallel Processing),即大規模并行處理,在資料庫非共享集群中,每個節點都有獨立的磁盤存盤系統和記憶體系統,業務資料根據資料庫模型和應用特點劃分到各個節點上,每臺資料節點通過專用網路或者商業通用網路互相連接,彼此協同計算,作為整體提供 ......

    uj5u.com 2020-09-10 06:11:41 more
  • 滴滴資料倉庫指標體系建設實踐

    **桔妹導讀:**指標體系是什么?如何使用OSM模型和AARRR模型搭建指標體系?如何統一流程、規范化、工具化管理指標體系?本文會對建設的方法論結合滴滴資料指標體系建設實踐進行解答分析。 #1. 什么是指標體系 ##1.1 指標體系定義 指標體系是將零散單點的具有相互聯系的指標,系統化的組織起來,通 ......

    uj5u.com 2020-09-10 06:12:52 more
  • 單表千萬行資料庫 LIKE 搜索優化手記

    我們經常在資料庫中使用 LIKE 運算子來完成對資料的模糊搜索,LIKE 運算子用于在 WHERE 子句中搜索列中的指定模式。 如果需要查找客戶表中所有姓氏是“張”的資料,可以使用下面的 SQL 陳述句: SELECT * FROM Customer WHERE Name LIKE '張%' 如果需要 ......

    uj5u.com 2020-09-10 06:13:25 more
  • 滴滴Ceph分布式存盤系統優化之鎖優化

    **桔妹導讀:**Ceph是國際知名的開源分布式存盤系統,在工業界和學術界都有著重要的影響。Ceph的架構和演算法設計發表在國際系統領域頂級會議OSDI、SOSP、SC等上。Ceph社區得到Red Hat、SUSE、Intel等大公司的大力支持。Ceph是國際云計算領域應用最廣泛的開源分布式存盤系統, ......

    uj5u.com 2020-09-10 06:14:51 more
  • es~通過ElasticsearchTemplate進行聚合~嵌套聚合

    之前寫過《es~通過ElasticsearchTemplate進行聚合操作》的文章,這一次主要寫一個嵌套的聚合,例如先對sex集合,再對desc聚合,最后再對age求和,共三層嵌套。 Aggregations的部分特性類似于SQL語言中的group by,avg,sum等函式,Aggregation ......

    uj5u.com 2020-09-10 06:14:59 more
  • 爬蟲日志監控 -- Elastc Stack(ELK)部署

    傻瓜式部署,只需替換IP與用戶 導讀: 現ELK四大組件分別為:Elasticsearch(核心)、logstash(處理)、filebeat(采集)、kibana(可視化) 下載均在https://www.elastic.co/cn/downloads/下tar包,各組件版本最好一致,配合fdm會 ......

    uj5u.com 2020-09-10 06:15:05 more
最新发布
  • day02-2-商鋪查詢快取

    功能02-商鋪查詢快取 3.商鋪詳情快取查詢 3.1什么是快取? 快取就是資料交換的緩沖區(稱作Cache),是存盤資料的臨時地方,一般讀寫性能較高。 快取的作用: 降低后端負載 提高讀寫效率,降低回應時間 快取的成本: 資料一致性成本 代碼維護成本 運維成本 3.2需求說明 如下,當我們點擊商店詳 ......

    uj5u.com 2023-04-20 08:33:24 more
  • MySQL中binlog備份腳本分享

    關于MySQL的二進制日志(binlog),我們都知道二進制日志(binlog)非常重要,尤其當你需要point to point災難恢復的時侯,所以我們要對其進行備份。關于二進制日志(binlog)的備份,可以基于flush logs方式先切換binlog,然后拷貝&壓縮到到遠程服務器或本地服務器 ......

    uj5u.com 2023-04-20 08:28:06 more
  • day02-短信登錄

    功能實作02 2.功能01-短信登錄 2.1基于Session實作登錄 2.1.1思路分析 2.1.2代碼實作 2.1.2.1發送短信驗證碼 發送短信驗證碼: 發送驗證碼的介面為:http://127.0.0.1:8080/api/user/code?phone=xxxxx<手機號> 請求方式:PO ......

    uj5u.com 2023-04-20 08:27:27 more
  • 快取與資料庫雙寫一致性幾種策略分析

    本文將對幾種快取與資料庫保證資料一致性的使用方式進行分析。為保證高并發性能,以下分析場景不考慮執行的原子性及加鎖等強一致性要求的場景,僅追求最終一致性。 ......

    uj5u.com 2023-04-20 08:26:48 more
  • sql陳述句優化

    問題查找及措施 問題查找 需要找到具體的代碼,對其進行一對一優化,而非一直把關注點放在服務器和sql平臺 降低簡化每個事務中處理的問題,盡量不要讓一個事務拖太長的時間 例如檔案上傳時,應將檔案上傳這一步放在事務外面 微軟建議 4.啟動sql定時執行計劃 怎么啟動sqlserver代理服務-百度經驗 ......

    uj5u.com 2023-04-20 08:26:35 more
  • 云時代,MySQL到ClickHouse資料同步產品對比推薦

    ClickHouse 在執行分析查詢時的速度優勢很好的彌補了MySQL的不足,但是對于很多開發者和DBA來說,如何將MySQL穩定、高效、簡單的同步到 ClickHouse 卻很困難。本文對比了 NineData、MaterializeMySQL(ClickHouse自帶)、Bifrost 三款產品... ......

    uj5u.com 2023-04-20 08:26:29 more
  • sql陳述句優化

    問題查找及措施 問題查找 需要找到具體的代碼,對其進行一對一優化,而非一直把關注點放在服務器和sql平臺 降低簡化每個事務中處理的問題,盡量不要讓一個事務拖太長的時間 例如檔案上傳時,應將檔案上傳這一步放在事務外面 微軟建議 4.啟動sql定時執行計劃 怎么啟動sqlserver代理服務-百度經驗 ......

    uj5u.com 2023-04-20 08:25:13 more
  • Redis 報”OutOfDirectMemoryError“(堆外記憶體溢位)

    Redis 報錯“OutOfDirectMemoryError(堆外記憶體溢位) ”問題如下: 一、報錯資訊: 使用 Redis 的業務介面 ,產生 OutOfDirectMemoryError(堆外記憶體溢位),如圖: 格式化后的報錯資訊: { "timestamp": "2023-04-17 22: ......

    uj5u.com 2023-04-20 08:24:54 more
  • day02-2-商鋪查詢快取

    功能02-商鋪查詢快取 3.商鋪詳情快取查詢 3.1什么是快取? 快取就是資料交換的緩沖區(稱作Cache),是存盤資料的臨時地方,一般讀寫性能較高。 快取的作用: 降低后端負載 提高讀寫效率,降低回應時間 快取的成本: 資料一致性成本 代碼維護成本 運維成本 3.2需求說明 如下,當我們點擊商店詳 ......

    uj5u.com 2023-04-20 08:24:03 more
  • day02-短信登錄

    功能實作02 2.功能01-短信登錄 2.1基于Session實作登錄 2.1.1思路分析 2.1.2代碼實作 2.1.2.1發送短信驗證碼 發送短信驗證碼: 發送驗證碼的介面為:http://127.0.0.1:8080/api/user/code?phone=xxxxx<手機號> 請求方式:PO ......

    uj5u.com 2023-04-20 08:23:11 more