某个打印机根据打印队列执行打印任务。打印任务分为九个优先级,分别用数字1~9表示,数字越大优先级越高。打印机每次从队列头部取出第一个任务A,然后检查队列余下任务中有没有比A优先级更高的任务,如果有比A优先级高的任务,则将任务A放到队列尾部,否则就执行任务A的打印。
请编写一个程序,根据输入的打印队列,输出实际的打印顺序。
输入描述:
输出描述:
输出为一个表示实际打印顺序的数组,其数组项为打印任务在输入数组中的索引值(从0开始)。Java通过返回值输出,C/C++通过输出参数output输出,可以假定为存放结果分配了足够的空间。
示例1
输入
9, 3, 5
输出
0, 2, 1
代码:
#include <iostream>
#include <stdio.h>
#include <list>
#include <vector>
using namespace std;
struct INPUT
{
int order;
int value;
};
void printOrder(const int input[], int len, int output[])
{
list<INPUT> tmplist;
int pos=0;
for (int i = 0; i < len; i++)
{
INPUT node;
node.order = i;
node.value = input[i];
tmplist.push_back(node);
}
while (tmplist.size() > 0)
{
bool flag = 0;
INPUT head = tmplist.front();
list<INPUT>::iterator it;
for (it = tmplist.begin(); it != tmplist.end(); ++it)
{
if (head.value < it->value)
{
flag = 1;
break;
}
}
if (flag)
{
tmplist.pop_front();
tmplist.push_back(head);
}
else
{
output[pos++]=head.order;
tmplist.pop_front();
}
}
return;
}
int main (void)
{
}
ps:这种机考题是真滴难
算法只占能力的1%,考虑输入占了99%。
又不给个测试用例,哪里跪了都不知道
输入格式和输出格式才是这个考试的重中之重!!!!!