数据结构编程题
银行排队模拟。下表所示,为14名用户到达的时间和办理业务预计消耗的时间。序号到达时间办理时间序号到达时间办理时间
1 9:00 8min 8 9:24 15min
2 9:00 3min 9 9:35 43min
3 9:03 18min 10 9:39 20min
4 9:05 20min 11 9:40 12min
5 9:06 38min 12 9:41 12min
6 9:10 8min 13 9:45 4min
7 9:20 12min 14 9:45 28min
设计并编写程序,分别求得只有一个窗口办理业务时,最后一名客户离开的时间;有两
个窗口办理业务时,最后一名客户离开的时间;两个窗口办理业务时,如果按业务预计
消耗时间将客户分别安排到两个窗口(20min以内在窗口1办理,20min以上的在窗口
2办理),最后一名客户离开的时间。用code block编写,c语言版。 #include <iostream>
#include <vector>
#include <utility>
using namespace std;
class Time {
friend ostream& operator<<(ostream& os, const Time&);
public:
int h, m;
Time();
Time(int, int);
inline void add(int);
bool operator<(const Time& time);
void operator=(const Time& time);
};
Time::Time(): h(0), m(0) { }
Time::Time(int hour, int minute): h(hour), m(minute) { }
ostream& operator<<(ostream& os, const Time& time) {
os << time.h << ":" << time.h;
return os;
}
void Time::add(int minute) {
m += minute;
h += m / 60;
m %= 60;
h %= 24;
}
bool Time::operator<(const Time& time) {
if (h < time.h) return true;
else if (h > time.h) return false;
return m < time.m;
}
bool operator<(const Time& A, const Time& B) {
if (A.h < B.h) return true;
else if (A.h > B.h) return false;
return A.m < B.m;
}
void Time::operator=(const Time& time) {
h = time.h;
m = time.m;
}
Time max(const Time& A, const Time& B) {
if (A < B) return B;
return A;
}
Time lastCall(vector<pair<Time, int>> service, bool twoWindows = false) {
Time last, A, B;
if (not twoWindows) {
for (const auto &: service) {
if (last < T) last = T;
last.add(m);
}
}
else {
for (const auto &: service) {
if (m <= 20) {
if (A < T) A = T;
A.add(m);
}
else {
if (B < T) B = T;
B.add(m);
}
}
last = max(A, B);
}
return last;
}
int main(void) {
vector<pair<Time, int>> service {
{make_pair(Time(9, 0), 8)},
{make_pair(Time(9, 0), 3)},
{make_pair(Time(9, 3), 18)},
{make_pair(Time(9, 5), 20)},
{make_pair(Time(9, 6), 38)},
{make_pair(Time(9, 10), 8)},
{make_pair(Time(9, 20), 12)},
{make_pair(Time(9, 24), 15)},
{make_pair(Time(9, 35), 43)},
{make_pair(Time(9, 39), 20)},
{make_pair(Time(9, 40), 12)},
{make_pair(Time(9, 41), 12)},
{make_pair(Time(9, 45), 4)},
{make_pair(Time(9, 45), 28)},
};
cout << lastCall(service) << endl;
cout << lastCall(service, true) << endl;
return 0;
}13:13
11:11
页:
[1]