DS队列----银行单队列多窗口模拟

it2025-03-31  9

题目描述 假设银行有K个窗口提供服务,窗口前设一条黄线,所有顾客按到达时间在黄线后排成一条长龙。 当有窗口空闲时,下一位顾客即去该窗口处理事务。当有多个窗口可选择时,假设顾客总是选择编号最小的窗口。

本题要求输出前来等待服务的N位顾客的平均等待时间、最长等待时间、最后完成时间。

输入 输入第1行给出正整数N(≤1000),为顾客总人数; 随后N行,每行给出一位顾客的到达时间T和事务处理时间P,并且假设输入数据已经按到达时间先后排好了顺序;最后一行给出正整数K(≤10),为开设的营业窗口数。

输出 在一行中输出平均等待时间(输出到小数点后1位)、最长等待时间、最后完成时间,之间用1个空格分隔,行末不能有多余空格。

样例输入 9 0 20 1 15 1 61 2 10 10 5 10 3 30 18 31 25 31 2 3 样例输出 6.2 17 62

#include <iostream> #include <queue> #include <iomanip> #include <string> using namespace std; class customer { public: int come_time,use_time; customer(int c_t,int u_t) { come_time = c_t; use_time = u_t; } }; class window { public: bool is_busy; int finish_time; window():is_busy(0),finish_time(0) {} }; int main() { int n, come_time, use_time, k; int all_wait_time = 0, wait_time = 0, finish_time = 0, max_wait_time = 0; queue<customer> cus; cin>>n; //顾客人数 int n1 = n; while(n1--) { cin>>come_time>>use_time; cus.push(customer(come_time,use_time)); } cin>>k; //窗口个数 window *win = new window[k]; int clock = 0; while(!cus.empty()) { if(clock >= cus.front().come_time) { //顾客到达窗口 for(int i = 0; i < k; i++) { if(win[i].finish_time <= clock) { //重置每个窗口的空闲状态 win[i].is_busy = 0; } } for(int i = 0; i < k; i++) { //判断每一个窗口是否空闲 if(!win[i].is_busy && (!cus.empty()) && (clock >= cus.front().come_time)) { win[i].is_busy = 1; win[i].finish_time = clock + cus.front().use_time; wait_time = clock - cus.front().come_time; all_wait_time += wait_time; if(wait_time > max_wait_time) { max_wait_time = wait_time; } if(win[i].finish_time > finish_time) { finish_time = win[i].finish_time; } cus.pop(); } } } clock++; } cout<< fixed << setprecision(1) <<(double) all_wait_time / n <<" "<<max_wait_time<<" "<<finish_time << endl; delete []win; return 0; }
最新回复(0)