打印任务排序
标题:打印任务排序 | 时间限制:1秒 | 内存限制:262144K | 语言限制:不限
某个打印机根据打印队列执行打印任务。打印任务分为九个优先级,分别用数字1~9表示,数字越大优先级越高。打印机每次从队列头部取出第一个任务A,然后检查队列余下任务中有没有比A优先级更高的任务,如果有比A优先级高的任务,则将任务A放到队列尾部,否则就执行任务A的打印。请编写一个程序,根据输入的打印队列,输出实际的打印顺序
from collections import deque
while True:
try:
nums = list(map(int, input().split(",")))
nums_list = list(enumerate(nums))
queue = deque()
for num in nums_list:
queue.append((num[0], num[1]))
ans, count = list(), 0
while True:
if len(queue) <= 1:
break
task = queue.popleft()
cur_max = max(queue, key=lambda x: x[1])
if task[1] >= cur_max[1]:
ans.append((task[0], count))
count += 1
else:
queue.append(task)
ans.append((queue[0][0], count))
print(",".join(map(lambda x: str(x[1]), sorted(ans, key=lambda x: x[0]))))
except:
break
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str[] = scanner.nextLine().split(",");
Queue<Integer> queue = new LinkedList<>();
int i = 0;
int h[] = new int[str.length];
for (String data : str) {
Integer tmp = Integer.valueOf(data);
queue.add(i);
h[i++] = tmp;
}
int k = 0;
int vis[] = new int[str.length];
Arrays.fill(vis, 0);
int ans[] = new int[str.length];
while (!queue.isEmpty()) {
int cur = queue.poll();
for (i = 0; i < str.length; i++) {
if (h[cur] < h[i] && vis[i] == 0) {
break;
}
}
if (i == str.length) {
ans[cur] = k++;
vis[cur] = 1;
} else {
queue.add(cur);
}
}
for (i = 0; i < str.length - 1; i++) {
System.out.print(ans[i] + ",");
}
System.out.println(ans[str.length - 1]);
}
}
//manfen
import sys
for line in sys.stdin:
rankQueue = line.strip().split(",")
def rank(rq):
top_rank = max(int(rank) for rank in rq)
rq_ls = ["" for i in range(len(rq))]
pos_ls = [i for i in range(len(rq))]
curr_pos = 0
curr_prior = 0
while rq:
if int(rq[0]) == top_rank:
rq_ls[pos_ls.pop(0)] = str(curr_prior)
curr_prior += 1
rq.pop(0)
if rq:
top_rank = max(int(rank) for rank in rq)
curr_pos = pos_ls[0]
else:
rq.append(rq.pop(0))
pos_ls.append(pos_ls.pop(0))
#pos_ls.append(curr_pos)
curr_pos = pos_ls[0]
return ",".join(rq_ls)
print(rank(rankQueue))
安克创新 Anker公司福利 728人发布