大家好,这里是国中之林!
❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看←
问题:
解答:
main.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <queue>
using namespace std;
const int MIN_PER_HR = 60;
class Customer
{
private:
long arrive;
int processtime;
public:
Customer() :arrive(0), processtime(0){}
void set(long when);
long when()const { return arrive; }
int ptime()const { return processtime; }
};
void Customer::set(long when)
{
processtime = rand() % 3 + 1;
arrive = when;
}
typedef Customer Item;
bool newcustomer(double x);
int main()
{
srand(time(0));
cout << "Case Study: Bank of Heather Automatic Teller\n";
cout << "Enter maximum size of queue: ";
int qs;
cin >> qs;
queue<Item>line;
cout << "Enter maximum size of simulation hours: ";
int hours;
cin >> hours;
long cyclelimit = MIN_PER_HR * hours;
cout << "Enter the average number of customers per hour: ";
double perhour;
cin >> perhour;
double min_per_cust;
min_per_cust = MIN_PER_HR / perhour;
Item temp;
long turnaways = 0;
long customers = 0;
long served = 0;
long sum_line = 0;
long wait_time = 0;
long line_wait = 0;
for (int cycle = 0; cycle < cyclelimit; cycle++)
{
if (newcustomer(min_per_cust))
{
if (line.size() >= qs)
{
turnaways++;
}
else
{
customers++;
temp.set(cycle);
line.push(temp);
}
}
if (wait_time <= 0 && !line.empty())
{
line.pop();
wait_time = temp.ptime();
line_wait += cycle - temp.when();
served++;
}
if (wait_time > 0)
{
wait_time--;
}
sum_line += line.size();
}
if (customers > 0)
{
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";
}
else
{
cout << "No customer!\n";
}
cout << "Done!\n";
return 0;
}
bool newcustomer(double x)
{
return (rand() * x / RAND_MAX < 1);
}
运行结果:
考查点:
- queue容器
- pop()出队
- push()入队
2024年9月17日21:09:00
标签:cout,temp,long,16.6,time,习题,Primer,line,wait From: https://blog.csdn.net/qq_74047911/article/details/142318875