news 2026/4/25 1:55:48

新卷-打印文件(C++ Python JAVA JS C语言)最佳实现

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
新卷-打印文件(C++ Python JAVA JS C语言)最佳实现

题目描述:
有5台打印机打印文件,每台打印机有自己的待打印队列。因为打印的文件内容有轻重缓急之分,所以队列中的文件有1~10不同的优先级一,其中数字越大优先级越高。打印机会从自己的待打印队列中选择优先级最高的文件来打印。如果存在两个优先级一样的文件,则选择最早进入队列的那个文件。
现在请你来模拟这5台打印机的打印过程。
输入描述:每个输入包含1个测试用例一,每个测试用例第1行给出发生事件的数量N(O<N <1000)。接下来有N行,分别表示发生的事件。
共有如下两种事件:
1."N PNUM",表示有一个拥有优先级NUM的文件放到了打印机Р的待打印队列中。(0<P <= 5,0<NUM<= 10);
2."OUTP",表示打印机Р进行了一次文件打印,同时该文件从待打印队列中取出。(0<P <= 5)。输出描述:
对于每个测试用例,每次"OUTP"事件,请在一行中输出文件的编号。如果此时没有文件可以打印
请输
出"NULL"。
文件的编号定义为:"IN PNUM"事件发生第×次,此处待打印文件的编号为x。编号从1开始。

示例1

输入:
7
IN 1 1

IN 1 2

IN 1 3

IN 21

OUT 1

OUT 2

OUT 2
输出:
3

4
NULL

解题思路

需要模拟5台打印机的打印队列,处理两种事件:文件入队(IN)和打印出队(OUT)。对于OUT事件,需要从指定打印机的队列中取出优先级最高的文件(数字越大优先级越高),优先级相同时选择最早入队的文件。

关键步骤

  1. 数据结构选择:每台打印机使用一个优先队列(或普通队列+排序)来管理文件,队列中的元素需要记录文件的编号和优先级。
  2. 事件处理:根据输入的事件类型分别处理:
    • IN事件:将文件加入对应打印机的队列,并记录文件编号。
    • OUT事件:从对应打印机的队列中取出优先级最高的文件(或NULL)。
  3. 优先级处理:在队列中,优先级高的文件先出队,优先级相同时先入队的先出队。

代码实现

C++ 实现

使用优先队列(priority_queue)自定义排序规则:

#include <iostream> #include <queue> #include <vector> using namespace std; struct File { int id; int priority; int seq; // 入队顺序 }; struct Compare { bool operator()(const File& a, const File& b) { if (a.priority != b.priority) { return a.priority < b.priority; } return a.seq > b.seq; } }; int main() { int N; cin >> N; vector<priority_queue<File, vector<File>, Compare>> printers(5); int fileId = 1; for (int i = 0; i < N; ++i) { string op; cin >> op; if (op == "IN") { int p, num; cin >> p >> num; printers[p - 1].push({fileId, num, fileId}); fileId++; } else if (op == "OUT") { int p; cin >> p; if (!printers[p - 1].empty()) { File file = printers[p - 1].top(); printers[p - 1].pop(); cout << file.id << endl; } else { cout << "NULL" << endl; } } } return 0; }
Python 实现

使用堆(heapq)模拟优先队列:

import heapq class File: def __init__(self, id, priority, seq): self.id = id self.priority = priority self.seq = seq def __lt__(self, other): if self.priority != other.priority: return self.priority > other.priority return self.seq < other.seq def main(): N = int(input()) printers = [[] for _ in range(5)] file_id = 1 for _ in range(N): op = input().split() if op[0] == "IN": p = int(op[1]) - 1 num = int(op[2]) heapq.heappush(printers[p], File(file_id, num, file_id)) file_id += 1 elif op[0] == "OUT": p = int(op[1]) - 1 if printers[p]: file = heapq.heappop(printers[p]) print(file.id) else: print("NULL") if __name__ == "__main__": main()
Java 实现

使用PriorityQueue自定义排序:

import java.util.*; class File { int id; int priority; int seq; public File(int id, int priority, int seq) { this.id = id; this.priority = priority; this.seq = seq; } } public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); PriorityQueue<File>[] printers = new PriorityQueue[5]; for (int i = 0; i < 5; i++) { printers[i] = new PriorityQueue<>((a, b) -> { if (a.priority != b.priority) { return b.priority - a.priority; } return a.seq - b.seq; }); } int fileId = 1; for (int i = 0; i < N; i++) { String op = sc.next(); if (op.equals("IN")) { int p = sc.nextInt() - 1; int num = sc.nextInt(); printers[p].add(new File(fileId, num, fileId)); fileId++; } else if (op.equals("OUT")) { int p = sc.nextInt() - 1; if (!printers[p].isEmpty()) { File file = printers[p].poll(); System.out.println(file.id); } else { System.out.println("NULL"); } } } } }
JavaScript 实现

使用数组模拟优先队列:

class File { constructor(id, priority, seq) { this.id = id; this.priority = priority; this.seq = seq; } } function main() { const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); let N; const printers = Array.from({ length: 5 }, () => []); let fileId = 1; let lineCount = 0; rl.on('line', (line) => { if (lineCount === 0) { N = parseInt(line); lineCount++; } else { const parts = line.split(' '); const op = parts[0]; if (op === 'IN') { const p = parseInt(parts[1]) - 1; const num = parseInt(parts[2]); printers[p].push(new File(fileId, num, fileId)); fileId++; } else if (op === 'OUT') { const p = parseInt(parts[1]) - 1; if (printers[p].length > 0) { printers[p].sort((a, b) => { if (a.priority !== b.priority) { return b.priority - a.priority; } return a.seq - b.seq; }); const file = printers[p].shift(); console.log(file.id); } else { console.log('NULL'); } } lineCount++; if (lineCount > N) { rl.close(); } } }); } main();
C 实现

使用数组模拟优先队列:

#include <stdio.h> #include <string.h> typedef struct { int id; int priority; int seq; } File; File printers[5][1000]; int sizes[5] = {0}; int main() { int N; scanf("%d", &N); int fileId = 1; for (int i = 0; i < N; i++) { char op[5]; scanf("%s", op); if (strcmp(op, "IN") == 0) { int p, num; scanf("%d %d", &p, &num); printers[p - 1][sizes[p - 1]].id = fileId; printers[p - 1][sizes[p - 1]].priority = num; printers[p - 1][sizes[p - 1]].seq = fileId; sizes[p - 1]++; fileId++; } else if (strcmp(op, "OUT") == 0) { int p; scanf("%d", &p); if (sizes[p - 1] == 0) { printf("NULL\n"); continue; } int maxIdx = 0; for (int j = 1; j < sizes[p - 1]; j++) { if (printers[p - 1][j].priority > printers[p - 1][maxIdx].priority) { maxIdx = j; } else if (printers[p - 1][j].priority == printers[p - 1][maxIdx].priority) { if (printers[p - 1][j].seq < printers[p - 1][maxIdx].seq) { maxIdx = j; } } } printf("%d\n", printers[p - 1][maxIdx].id); for (int j = maxIdx; j < sizes[p - 1] - 1; j++) { printers[p - 1][j] = printers[p - 1][j + 1]; } sizes[p - 1]--; } } return 0; }

总结

  • 数据结构:优先队列(或排序后的数组)是解决优先级问题的关键。
  • 事件处理:区分IN和OUT事件,分别处理入队和出队逻辑。
  • 优先级规则:数字越大优先级越高,相同时选择最早入队的文件。
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/18 0:56:57

Foundation 网格 - 大型设备

Foundation 网格系统在大型设备&#xff08;Large Devices&#xff09;上的行为 Foundation XY Grid 的 large 断点 默认对应屏幕宽度 ≥ 1024px&#xff08;通常指桌面电脑、大型平板横屏或宽屏显示器&#xff09;。 移动优先原则&#xff1a;如果没有指定 large-* 类&#…

作者头像 李华
网站建设 2026/4/24 11:21:12

Avalonia源码解读:Grid(网格控件)

在各类XAML UI框架中&#xff0c;Grid 是一种非常灵活且常用的布局控件&#xff0c;它可以创建复杂的用户界面布局。Grid 允许开发者通过定义行和列来组织界面元素&#xff0c;每个元素可以精确地放置在网格的特定区域内 本文以 Avalonia 框架为例&#xff0c;讲解 Grid 控件的…

作者头像 李华
网站建设 2026/4/18 12:34:51

Spring Integration 轻松实现服务间消息传递,真香!

&#x1f449; 这是一个或许对你有用的社群&#x1f431; 一对一交流/面试小册/简历优化/求职解惑&#xff0c;欢迎加入「芋道快速开发平台」知识星球。下面是星球提供的部分资料&#xff1a; 《项目实战&#xff08;视频&#xff09;》&#xff1a;从书中学&#xff0c;往事上…

作者头像 李华
网站建设 2026/4/23 18:14:10

阿帕他胺联合ADT治疗:快速深度降低PSA,为疾病控制提供重要指标

前列腺特异性抗原&#xff08;PSA&#xff09;作为前列腺癌患者随访过程中的一个重要指标&#xff0c;能够反映肿瘤的进展程度和药物的治疗效果。在TITAN研究中&#xff0c;阿帕他胺联合ADT治疗在降低PSA水平方面表现出了快速、深度的特点&#xff0c;为疾病的控制提供了重要的…

作者头像 李华
网站建设 2026/4/24 20:16:27

XML验证:处理XML Schema命名空间问题

在开发过程中,常常会遇到XML文档需要验证其结构是否符合预期的XSD(XML Schema Definition)。然而,当涉及到命名空间的使用时,可能会出现一些验证错误。本文将通过一个实际案例,详细解析XML验证中常见的问题——命名空间声明的错误及其解决方法。 背景介绍 假设我们正在…

作者头像 李华
网站建设 2026/4/23 19:08:53

OpenAI开源GPT-OSS-120B/20B混合专家模型

OpenAI开源GPT-OSS-120B/20B混合专家模型 在大模型军备竞赛愈演愈烈的今天&#xff0c;一个反向信号悄然浮现&#xff1a;性能不再唯一&#xff0c;可控性与部署效率正成为新的制高点。当多数厂商还在堆叠参数、追逐榜单时&#xff0c;OpenAI却选择将一扇门推开——正式开源了两…

作者头像 李华