赞
踩
题目:
Heather银行进行的研究表明,ATM客户不希望排队时间不超过1分钟。请使用书中程序清单12.10中的模拟,找出要使平均等候时间为1分钟,每小时到达的客户数应为多少(试验时间不短于100小时)?
思路:
通过网络搜索,分析网友的回答,解决这个问题有两种思路:
一、直接使用书中的例题源代码12.10、12.11和12.12,通过输入不同的参数,人工找出运行结果里平均等候时间为1分种的客户数。
(参考:1、http://zhidao.baidu.com/link?url=2GVmQkvQYIStDYIUDuxvCh_lib0CLHBjEwseIOPKkuIp-V6oLsrwtTyOmY5XKlsE8OaYAmLMshyDeANPXzw8ba 2、http://blog.csdn.net/qq844352155/article/details/24327151)
二、问题的关键在于对“平均等候时间为1分钟”条件的设定。简单修改一下12.12中的部分代码,通过设定条件,自动获取平均等候时间为1分钟的客户数(参考:http://tieba.baidu.com/p/1826136344)。我理解,不太可能完全获得平均值时间正好为1分钟的值,只能是非常接近。应用常用的知识点,使一个浮点数无限接近于某一个值(本题中是1.0)的办法,程序中使用了fabs((double)line_wait / served-1)<0.1 这个条件表达式。在测试过程中发现,设置的精度不能太高,否则得到的结果是每小时到达的客户接近或者60,那么客户到达的平均时间间隔少于1分钟,根据书中所述,此时是无效的。现将12.12的修改代码附上,结合书中的12.10、12.11即可运行。
- // bank.cpp -- using the Queue interface
- // compile with queue.cpp
- #include <iostream>
- #include <cstdlib> // for rand() and srand()
- #include <ctime> // for time()
- #include <cmath>
- #include "queue.h"
- const int MIN_PER_HR = 60;
-
- bool newcustomer(double x); // is there a new customer?
-
- int main()
- {
- using std::cin;
- using std::cout;
- using std::endl;
- using std::ios_base;
- // setting things up
- std::srand(std::time(0)); // random initializing of rand()
-
- cout << "Case Study: Bank of Heather Automatic Teller\n";
- cout << "Enter maximum size of queue: ";
- int qs;
- cin >> qs;
- Queue line(qs); // line queue holds up to qs people
-
- cout << "Enter the number of simulation hours: ";
- int hours; // hours of simulation
- cin >> hours;
- // simulation will run 1 cycle per minute
- long cyclelimit = MIN_PER_HR * hours; // # of cycles
-
- double perhour=1;
- double min_per_cust;
-
- long turnaways = 0; // turned away by full queue
- long customers = 0; // joined the queue
- long served = 0; // served during the simulation
- long sum_line = 0; // cumulative line length
- int wait_time = 0; // time until autoteller is free
- long line_wait = 0;
-
- while(1)
- {
- min_per_cust = MIN_PER_HR / perhour;
- Item temp; // new customer data
- turnaways = 0; // turned away by full queue
- customers = 0; // joined the queue
- served = 0; // served during the simulation
- sum_line = 0; // cumulative line length
- wait_time = 0; // time until autoteller is free
- line_wait = 0;
-
- for (int cycle = 0; cycle < cyclelimit; cycle++)
- {
- if (newcustomer(min_per_cust)) // have newcomer
- {
- if (line.isfull())
- turnaways++;
- else
- {
- customers++;
- temp.set(cycle); // cycle = time of arrival
- line.enqueue(temp); // add newcomer to line
- }
- }
- if (wait_time <= 0 && !line.isempty())
- {
- line.dequeue (temp); // attend next customer
- wait_time = temp.ptime(); // for wait_time minutes
- line_wait += cycle - temp.when();
- served++;
- }
- if (wait_time > 0)
- wait_time--;
- sum_line += line.queuecount();
- }
-
- if(fabs((double)line_wait / served-1.0)<0.1)
- {
- cout << "customers accepted: " << customers << endl;
- cout << " customers served: " << served << endl;
- cout << " turnaways: " << turnaways << endl;
- cout << "average queue size: ";
- cout.precision(2);
- cout.setf(ios_base::fixed, ios_base::floatfield);
- cout << (double) sum_line / cyclelimit << endl;
- cout << "average wait time: "
- << (double) line_wait / served << " minutes\n";
- cout<<"fabs((double)line_wait / served-1.0):"<<fabs((double)line_wait / served-1.0)<<endl;
- cout<<"average number of customer per hour: "<<perhour<<endl;
-
-
- break;
- }
-
- perhour++;
-
- }
-
- cin.get();
- cin.get();
- return 0;
- }
-
- // x = average time, in minutes, between customers
- // return value is true if customer shows up this minute
- bool newcustomer(double x)
- {
- return (std::rand() * x / RAND_MAX < 1);
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。