自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C++數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之隊(duì)列的應(yīng)用

開(kāi)發(fā) 后端
在C++數(shù)據(jù)結(jié)構(gòu)中,隊(duì)列的應(yīng)用很廣泛。本文以銀行掛牌排號(hào)的營(yíng)業(yè)模式為例,模擬隊(duì)列運(yùn)用,詳述隊(duì)列的應(yīng)用的具體過(guò)程,為讀者學(xué)習(xí)C++隊(duì)列的應(yīng)用,提供一些參考。

  在學(xué)習(xí)C++隊(duì)列的運(yùn)用中,我看了兩本書(shū),都是講解隊(duì)列應(yīng)用的,而且都是銀行營(yíng)業(yè)模擬。細(xì)比較,這兩本書(shū)模擬的銀行營(yíng)業(yè)的方式還是不同的。老式的營(yíng)業(yè)模式,現(xiàn)在的很多地方還是這種營(yíng)業(yè)模式——幾個(gè)窗口同時(shí)排隊(duì)。這種方式其實(shí)不太合理,經(jīng)常會(huì)出現(xiàn)先來(lái)的還沒(méi)有后來(lái)的先辦理業(yè)務(wù)(常常前面一個(gè)人磨磨蹭蹭,別的隊(duì)越來(lái)越短,讓你恨不得把前面那人干掉)。另一種營(yíng)業(yè)模式——掛牌的營(yíng)業(yè)方式,每個(gè)來(lái)到的顧客發(fā)一個(gè)號(hào)碼,如果哪個(gè)柜臺(tái)空閑了,就叫號(hào)碼最靠前的顧客來(lái)辦理業(yè)務(wù);如果同時(shí)幾個(gè)柜臺(tái)空閑,就按照一種法則來(lái)決定這幾個(gè)柜臺(tái)叫號(hào)的順序(最簡(jiǎn)單的是按柜臺(tái)號(hào)碼順序)。這樣,就能保證顧客按照先來(lái)后到的順序接受服務(wù)——因?yàn)榇蠹遗旁谝粋€(gè)隊(duì)里。這樣的營(yíng)業(yè)模式我在北京的西直門(mén)工商銀行見(jiàn)過(guò),應(yīng)該說(shuō)這是比較合理的一種營(yíng)業(yè)模式。

  我按照實(shí)際情況模擬,實(shí)現(xiàn)如下:

  1. #ifndef Simulation_H  
  2. #define Simulation_H  
  3.  
  4. #include <iostream.h>  
  5. #include <stdlib.h>  
  6. #include <time.h>  
  7.  
  8.  
  9. class Teller  
  10. {  
  11.  public:  
  12.   int totalCustomerCount;  
  13.   int totalServiceTime;  
  14.   int finishServiceTime;  
  15.   Teller() :totalCustomerCount(0), totalServiceTime(0),  
  16.   finishServiceTime(0) {}  
  17. };  
  18.  
  19. //#define PRINTPROCESS  
  20.  
  21. class Simulation  
  22. {  
  23.  public:  
  24.   Simulation()  
  25.   {  
  26.    cout << endl << "輸入模擬參數(shù)" << endl;  
  27.    cout << "柜臺(tái)數(shù)量:"; cin >> tellerNum;  
  28.    cout << "營(yíng)業(yè)時(shí)間:"; cin >> simuTime;  
  29.    cout << "兩個(gè)顧客來(lái)到的最小間隔時(shí)間:"; cin >> arrivalLow;  
  30.    cout << "兩個(gè)顧客來(lái)到的最大間隔時(shí)間:"; cin >> arrivalHigh;  
  31.    cout << "柜臺(tái)服務(wù)最短時(shí)間:"; cin >> serviceLow;  
  32.    cout << "柜臺(tái)服務(wù)最長(zhǎng)時(shí)間:"; cin >> serviceHigh;  
  33.    arrivalRange = arrivalHigh - arrivalLow + 1;  
  34.    serviceRange = serviceHigh - serviceLow + 1;  
  35.    srand((unsigned)time(NULL));  
  36.   }  
  37.   Simulation(int tellerNum, int simuTime, int arrivalLow, int arrivalHigh, int serviceLow, int serviceHigh)  
  38.  
  39. : tellerNum(tellerNum), simuTime(simuTime), arrivalLow(arrivalLow), arrivalHigh(arrivalHigh),  
  40.  
  41.   serviceLow(serviceLow), serviceHigh(serviceHigh),  
  42.   arrivalRange(arrivalHigh - arrivalLow + 1), serviceRange(serviceHigh - serviceLow + 1)  
  43.   { srand((unsigned)time(NULL)); }  
  44.  
  45. void Initialize()  
  46. {  
  47.  curTime = nextTime = 0;  
  48.  customerNum = customerTime = 0;  
  49.  for (int i = 1; i <= tellerNum; i++)  
  50.  {  
  51.   tellers[i].totalCustomerCount = 0;  
  52.   tellers[i].totalServiceTime = 0;  
  53.   tellers[i].finishServiceTime = 0;  
  54.  }  
  55.  customer.MakeEmpty();  
  56. }  
  57.  
  58. void Run()  
  59. {  
  60.  Initialize();   
  61.  NextArrived();  
  62.  #ifdef PRINTPROCESS  
  63.  
  64.   cout << endl;  
  65.   cout << "tellerID";  
  66.   for (int k = 1; k <= tellerNum; k++) cout << "\tTELLER " << k;  
  67.   cout << endl;  
  68.  #endif  
  69.  
  70.  for (curTime = 0; curTime <= simuTime; curTime++)  
  71.  {  
  72.   if (curTime >= nextTime)  
  73.   {  
  74.    CustomerArrived();  
  75.    NextArrived();  
  76.   }  
  77.   #ifdef PRINTPROCESS   
  78.    cout << "Time: " << curTime << " ";  
  79.   #endif  
  80.   for (int i = 1; i <= tellerNum; i++)  
  81.   {  
  82.    if (tellers[i].finishServiceTime < curTime) tellers[i].finishServiceTime = curTime;  
  83.    if (tellers[i].finishServiceTime == curTime && !customer.IsEmpty())  
  84.    {  
  85.     int t = NextService();  
  86.     #ifdef PRINTPROCESS   
  87.      cout << '\t' << customerNum + 1 << '(' << customer.GetFront() << ',' << t << ')';  
  88.     #endif  
  89.     CustomerDeparture();  
  90.     tellers[i].totalCustomerCount++;  
  91.     tellers[i].totalServiceTime += t;  
  92.     tellers[i].finishServiceTime += t;  
  93.    }  
  94.  
  95.    #ifdef PRINTPROCESS   
  96.    else cout << "\t ";  
  97.    #endif  
  98.   }  
  99.   #ifdef PRINTPROCESS   
  100.    cout << endl;  
  101.   #endif  
  102.  }  
  103.  PrintResult();  
  104. }  
  105.  
  106. void PtintSimuPara()  
  107. {  
  108.  cout << endl << "模擬參數(shù)" << endl;  
  109.  cout << "柜臺(tái)數(shù)量: " << tellerNum << "\t營(yíng)業(yè)時(shí)間:" << simuTime << endl;  
  110.  cout << "兩個(gè)顧客來(lái)到的最小間隔時(shí)間:" << arrivalLow << endl;  
  111.  cout << "兩個(gè)顧客來(lái)到的最大間隔時(shí)間:" << arrivalHigh << endl;;  
  112.  cout << "柜臺(tái)服務(wù)最短時(shí)間:" << serviceLow << endl;  
  113.  cout << "柜臺(tái)服務(wù)最長(zhǎng)時(shí)間:" << serviceHigh << endl;  
  114. }  
  115.  
  116. void PrintResult()  
  117. {  
  118.  int tSN = 0;  
  119.  long tST = 0;  
  120.  cout << endl;  
  121.  cout << "-------------模擬結(jié)果-------------------";  
  122.  cout << endl << "tellerID\tServiceNum\tServiceTime\tAverageTime" << endl;  
  123.  for (int i = 1; i <= tellerNum; i++)  
  124.  {  
  125.   cout << "TELLER " << i;  
  126.   cout << '\t' << tellers[i].totalCustomerCount << " "; tSN += tellers[i].totalCustomerCount;  
  127.   cout << '\t' << tellers[i].totalServiceTime << " "; tST += (long)tellers[i].totalServiceTime;  
  128.  
  129.   cout << '\t';  
  130.   if (tellers[i].totalCustomerCount)  
  131.    cout << (float)tellers[i].totalServiceTime/(float)tellers[i].totalCustomerCount;  
  132.   else cout << 0;  
  133.    cout << " " << endl;  
  134.  }  
  135.  cout << "TOTAL \t" << tSN << " \t" << tST << " \t";  
  136.  if (tSN) cout << (float)tST/(float)tSN; else cout << 0;  
  137.  cout << " " << endl;  
  138.  cout << "Customer Number:\t" << customerNum << "\tno Service:\t" << customerNum - tSN << endl;  
  139.  
  140.  cout << "Customer WaitTime:\t" << customerTime << "\tAvgWaitTime:\t";  
  141.  if (tSN) cout << (float)customerTime/(float)tSN; else cout << 0;  
  142.  cout << endl;  
  143. }  
  144.  
  145. private:  
  146.  int tellerNum;  
  147.  int simuTime;  
  148.  int curTime, nextTime;  
  149.  int customerNum;  
  150.  long customerTime;  
  151.  int arrivalLow, arrivalHigh, arrivalRange;  
  152.  int serviceLow, serviceHigh, serviceRange;  
  153.  Teller tellers[21];  
  154.  Queue<int> customer;  
  155.  
  156.  void NextArrived()  
  157.  {  
  158.   nextTime += arrivalLow + rand() % arrivalRange;  
  159.  }  
  160.  
  161.  int NextService()  
  162.  {  
  163.   return serviceLow + rand() % serviceRange;  
  164.  }  
  165.  
  166. void CustomerArrived()  
  167. {  
  168.  customerNum++;  
  169.  customer.EnQueue(nextTime);  
  170. }  
  171.  
  172. void CustomerDeparture()  
  173. {  
  174.  customerTime += (long)curTime - (long)customer.DeQueue();  
  175. }  
  176.  
  177. };  
  178.  
  179. #endif 

  幾點(diǎn)說(shuō)明

  1、Run()的過(guò)程是這樣的:curTime是時(shí)鐘,從開(kāi)始營(yíng)業(yè)計(jì)時(shí),自然流逝到停止?fàn)I業(yè)。當(dāng)顧客到的事件發(fā)生時(shí)(顧客到時(shí)間等于當(dāng)前時(shí)間,小于判定是因?yàn)閭€(gè)別時(shí)候顧客同時(shí)到達(dá)——輸入arrivalLow=0的情況,而在同一時(shí)間,只給一個(gè)顧客發(fā)號(hào)碼),給這個(gè)顧客發(fā)號(hào)碼(用顧客到時(shí)間標(biāo)示這個(gè)顧客,入隊(duì),來(lái)到顧客數(shù)增1)。當(dāng)柜臺(tái)服務(wù)完畢時(shí)(柜臺(tái)服務(wù)完時(shí)間等于當(dāng)前時(shí)間),該柜臺(tái)服務(wù)人數(shù)增1,服務(wù)時(shí)間累加,顧客離開(kāi)事件發(fā)生,下一個(gè)顧客到該柜臺(tái)。因?yàn)楣衽_(tái)開(kāi)始都是空閑的,所以實(shí)際代碼和這個(gè)有點(diǎn)出入。最后,停止?fàn)I業(yè)的時(shí)候,停止發(fā)號(hào)碼,還在接受服務(wù)的顧客繼續(xù)到服務(wù)完,其他還在排隊(duì)的就散伙了。

  2、模擬結(jié)果分別是:各個(gè)柜臺(tái)的服務(wù)人數(shù)、服務(wù)時(shí)間、平均服務(wù)時(shí)間,總的服務(wù)人數(shù)、服務(wù)時(shí)間、平均服務(wù)時(shí)間,來(lái)的顧客總數(shù)、沒(méi)被服務(wù)的數(shù)目(來(lái)的太晚了)、接受服務(wù)顧客總等待時(shí)間、平均等待時(shí)間。

  3、這個(gè)算法效率是比較低的,實(shí)際上可以不用隊(duì)列完成這個(gè)模擬(用顧客到時(shí)間推動(dòng)當(dāng)前時(shí)鐘,柜臺(tái)直接公告服務(wù)完成時(shí)間),但這樣就和實(shí)際情況有很大差別了——出納員沒(méi)等看見(jiàn)人就知道什么時(shí)候完?雖然結(jié)果是一樣的,但是理解起來(lái)很莫名其妙,尤其是作為教學(xué)目的講解的時(shí)候。當(dāng)然了,實(shí)際中為了提高模擬效率,本文的這個(gè)算法是不值得提倡的。

  4、注釋掉的#define PRINTPROCESS,去掉注釋符后,在運(yùn)行模擬的時(shí)候,能打印出每個(gè)時(shí)刻柜臺(tái)的服務(wù)情況(第幾個(gè)顧客,顧客到達(dá)時(shí)間,接受服務(wù)時(shí)間),但只限4個(gè)柜臺(tái)以下,多了的話(huà)屏幕就滿(mǎn)了(格式就亂了)。

  這是數(shù)據(jù)結(jié)構(gòu)中第一個(gè)實(shí)際應(yīng)用的例子,而且也有現(xiàn)實(shí)意義。你可以看出各個(gè)柜臺(tái)在不同的業(yè)務(wù)密度下的工作強(qiáng)度(要么給哪個(gè)柜臺(tái)出納員發(fā)獎(jiǎng)金,要么輪換柜臺(tái)),各種情況下顧客的等待時(shí)間(人都是輪到自己就不著急了),還有各種情況下設(shè)立幾個(gè)柜臺(tái)合理(很少的空閑時(shí)間,很短的等待時(shí)間,幾乎為零的未服務(wù)人數(shù))。例如這樣:

  1. for (int i = 1; i < 16; i++)  
  2. {  
  3.  Simulation a(i,240,1,4,8,15);  
  4.  a.Run();  
  5. }  

  你模擬一下就會(huì)得出,在不太繁忙的銀行,4~5個(gè)柜臺(tái)是合適的——現(xiàn)在的銀行大部分都是這樣的。

【編輯推薦】

  1. 18.3.1 隊(duì)列的概念 
  2. 數(shù)據(jù)庫(kù)使用C++數(shù)據(jù)結(jié)構(gòu)
  3. 程序員必看 c++筆試題匯總
  4. C++數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之棧的應(yīng)用
  5. C++數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之棧和隊(duì)列
責(zé)任編輯:韓亞珊 來(lái)源: 天極網(wǎng)
相關(guān)推薦

2011-04-11 11:23:17

隊(duì)列數(shù)據(jù)結(jié)構(gòu)

2011-04-11 12:22:11

數(shù)據(jù)結(jié)構(gòu)C++

2011-04-11 17:09:37

稀疏矩陣矩陣C++

2022-03-31 11:17:58

JavaScript數(shù)組方法

2012-02-02 10:21:05

單鏈表nexthead

2021-07-16 07:57:34

Python數(shù)據(jù)結(jié)構(gòu)

2009-08-11 14:43:42

C#數(shù)據(jù)結(jié)構(gòu)與算法

2009-08-13 16:02:29

C#結(jié)構(gòu)

2010-01-27 15:58:35

C++數(shù)據(jù)結(jié)構(gòu)

2023-12-13 10:01:15

數(shù)據(jù)結(jié)構(gòu)c++編程

2021-06-08 06:01:00

C++數(shù)據(jù)結(jié)構(gòu)向量和數(shù)組

2024-01-15 06:01:36

C++數(shù)組

2010-07-19 11:07:13

Perl控制結(jié)構(gòu)

2009-08-12 18:35:17

C#數(shù)據(jù)結(jié)構(gòu)

2011-07-20 17:10:54

C++

2022-09-01 16:27:19

JavaScriptWeb開(kāi)發(fā)

2018-06-13 08:53:39

HadoopHBase存儲(chǔ)

2023-03-28 07:44:23

數(shù)據(jù)結(jié)構(gòu)數(shù)組

2009-08-11 14:51:11

C#數(shù)據(jù)結(jié)構(gòu)與算法

2020-12-17 10:12:33

數(shù)據(jù)結(jié)構(gòu)算法隊(duì)列
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)