设某银行有A、B两个业务窗口,且处理业务的速度不一样,其中A窗口处理速度是B窗口的2倍,即当A窗口每处理完2个顾客时,B窗口处理完1个顾客。给定到达银行的顾客序列,请按业务完成的顺序输出顾客序列。假定不考虑顾客先后到达的时间间隔,并且当不同窗口同时处理完2个顾客时,A窗口优先输出。 #include using namespace std;
class MyQueue { public: MyQueue(); ~MyQueue(); void enQueue(int data); int getHead(); int deQueue(); void clear(); bool isEmpty();
private: int front; int rear; int queue[100]; };
MyQueue::MyQueue() { front = 0; rear = 0;
for (int i = 0; i < 100; i++) { queue[i] = 0; }}
MyQueue::~MyQueue() { }
void MyQueue::enQueue(int data) { queue[++rear] = data; }
int MyQueue::getHead() { if (rear == front) { throw “None”; }
return queue[front + 1];}
int MyQueue::deQueue() { if (rear == front) { throw “None”; }
return queue[++front];}
void MyQueue::clear() { front = ++rear; }
bool MyQueue::isEmpty() { return rear == front; }
int main() { MyQueue A; MyQueue B;
int amounts = 0; cin >> amounts; int number[20]; for (int i = 0; i < amounts; i++) { cin >> number[i]; } for (int i = 0; i < amounts; i++) { if (number[i] % 2 == 0) { B.enQueue(number[i]); } else { A.enQueue(number[i]); } } while (true) { try { cout << A.deQueue(); A.getHead(); cout << " "; } catch (const char* e) { break; } try { cout << A.deQueue(); A.getHead(); cout << " "; } catch (const char* e) { break; } try { cout << B.deQueue(); B.getHead(); cout << " "; } catch (const char* e) { break; } } if (A.isEmpty() && B.isEmpty()) { return 0; } else if (A.isEmpty()) { while (true) { try { cout << B.deQueue(); B.getHead(); cout << " "; } catch (const char* e) { break; } } } else { while (true) { try { cout << A.deQueue(); A.getHead(); cout << " "; } catch (const char* e) { break; } } } return 0;}