某个打印机根据打印队列执行打印任务。打印任务分为九个优先级,分别采用数字1~9表示,数字越大优先级越高。打印机每次从队列头部取出第一个任务A,然后检查队列余下任务中有没有比A优先级更高的任务,如果有比A优先级高的任务,则将任务A放到队列尾部,否则执行任务A的打印。请编写一个程序,根据输入的打印队列,输出实际打印顺序。
函数原型:
void printOrder(const int input[], int len, int output[])
参数input表示打印队列,为1~9(优先级)组成的数组,数组索引0代表头部。对于C/C++参数len代表input数组长度,假定参数合法有效;
#include <iostream>
#include <deque>
using namespace std;
void printOrder(const int input[], int len, int output[]) {
if (len <= 0) {
printf("Input Error");
return;
}
int printPosition = 0;
deque<int> de;
for (int i=0; i<len; ++i) {
de.push_back(input[i]);
}
while (de.size() > 0) {
int head = de.front();
bool bigFlag = false;
for (auto it=de.begin(); it!=de.end(); ++it) {
if (*it > head) {
bigFlag = true;
break;
}
}
if (bigFlag) {
de.push_back(head);
de.pop_front();
} else {
de.pop_front();
for (int i=0; i<len; ++i) {
if (input[i] == head) {
output[i] = printPosition;
++printPosition;
}
}
}
}
}
int main() {
int input[] = {9,3,5,5,6};
int output[5] = {0};
printOrder(input, 5, output);
for (int i=0; i<5; ++i) {
cout<<output[i];
}
return 0;
}