全新题目


【编程题目 |100分】打印机队列华为OD机试  2023 Q1考试题 A卷】


时间限制:C/C++ 1秒,其他语言 2秒

空间限制:C/C++262144K,其他语言524288K

64bit IO Format:%lld


本题可使用本地IDE编码,不能使用本地已有代码,无跳出限制,

编码后请点击”保存并调试“按钮进行代码提交。


题目描述

【打印机队列】

题目描述

有5台打印机打印文件,每台打印机有自己的待打印队列。因为打印的文件内容有轻重缓急之分,

所以队列中的文件有1~10不同的代先级,其中数字越大优先级越高

打印机会从自己的待打印队列中选择优先级最高的文件来打印。

如果存在两个优先级一样的文件,则选择最早进入队列的那个文件。

现在请你来模拟这5台打印机的打印过程。

输入描述

每个输入包含1个测试用例,每个测试用例第一行给出发生事件的数量N(0 < N < 1000)。
接下来有 N 行,分别表示发生的事件。
共有如下两种事件:
1. “IN P NUM”,表示有一个拥有优先级 NUM 的文件放到了打印机 P 的待打印队列中。(0< P <= 5, 0 < NUM <= 10);
2. “OUT P”,表示打印机 P 进行了一次文件打印,同时该文件从待打印队列中取出。(0 < P <= 5)。


原题界面



代码实现


C++解法一


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<bits/stdc++.h>
using namespace std;
typedef struct Event {
int p; // 文件优先级
int t; // 文件编号
Event(int p_, int t_) {
this->p = p_;
this->t = t_;
}
} Event;
#include<bits/stdc++.h> using namespace std; typedef struct Event { int p; // 文件优先级 int t; // 文件编号 Event(int p_, int t_) { this->p = p_; this->t = t_; } } Event;
#include<bits/stdc++.h>

using namespace std;

typedef struct Event {
    int p; // 文件优先级
    int t; // 文件编号

    Event(int p_, int t_) {
        this->p = p_;
        this->t = t_;
    }
} Event;

剩余50%内容,购买单篇文章或订阅会员后查看


隐藏内容

此处内容需要权限查看

  • 普通用户特权:11金币
  • 会员用户特权:免费
  • 永久会员用户特权:免费推荐
会员免费查看


C++解法二


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<bits/stdc++.h>
using namespace std;
typedef struct Event {
int p; // 文件优先级
int t; // 文件编号
Event(int p_, int t_) {
this->p = p_;
this->t = t_;
}
} Event;
#include<bits/stdc++.h> using namespace std; typedef struct Event { int p; // 文件优先级 int t; // 文件编号 Event(int p_, int t_) { this->p = p_; this->t = t_; } } Event;
#include<bits/stdc++.h>

using namespace std;

typedef struct Event {
    int p; // 文件优先级
    int t; // 文件编号

    Event(int p_, int t_) {
        this->p = p_;
        this->t = t_;
    }
} Event;

剩余50%内容,购买单篇文章或订阅会员后查看


隐藏内容

此处内容需要权限查看

  • 普通用户特权:11金币
  • 会员用户特权:免费
  • 永久会员用户特权:免费推荐
会员免费查看


JAVA解法一


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.*;
public class Main {
public static void main(String[] args) {
import java.util.*; public class Main { public static void main(String[] args) {
import java.util.*;

public class Main {
    public static void main(String[] args) {

剩余50%内容,购买单篇文章或订阅会员后查看


隐藏内容

此处内容需要权限查看

  • 普通用户特权:11金币
  • 会员用户特权:免费
  • 永久会员用户特权:免费推荐
会员免费查看


JAVA解法二


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.*;
public class Main {
public static void main(String[] args) {
import java.util.*; public class Main { public static void main(String[] args) {
import java.util.*;

public class Main {
    public static void main(String[] args) {

剩余50%内容,购买单篇文章或订阅会员后查看


隐藏内容

此处内容需要权限查看

  • 普通用户特权:11金币
  • 会员用户特权:免费
  • 永久会员用户特权:免费推荐
会员免费查看


Python解法一


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from typing import List
class Solution:
def PrinterQueue(self, n: int, priters: List[str]) -> str:
# 待实现函数,在此函数中填入答题代码
from typing import List class Solution: def PrinterQueue(self, n: int, priters: List[str]) -> str: # 待实现函数,在此函数中填入答题代码
from typing import List


class Solution:
    def PrinterQueue(self, n: int, priters: List[str]) -> str:
        # 待实现函数,在此函数中填入答题代码

剩余50%内容,购买单篇文章或订阅会员后查看


隐藏内容

此处内容需要权限查看

  • 普通用户特权:11金币
  • 会员用户特权:免费
  • 永久会员用户特权:免费推荐
会员免费查看

Python解法二


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from typing import List
class Solution:
def PrinterQueue(self, n: int, priters: List[str]) -> str:
# 待实现函数,在此函数中填入答题代码
from typing import List class Solution: def PrinterQueue(self, n: int, priters: List[str]) -> str: # 待实现函数,在此函数中填入答题代码
from typing import List


class Solution:
    def PrinterQueue(self, n: int, priters: List[str]) -> str:
        # 待实现函数,在此函数中填入答题代码

剩余50%内容,购买单篇文章或订阅会员后查看


隐藏内容

此处内容需要权限查看

  • 普通用户特权:11金币
  • 会员用户特权:免费
  • 永久会员用户特权:免费推荐
会员免费查看

JavaScript


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, });
let readline = require("readline");

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});

剩余50%内容,购买单篇文章或订阅会员后查看


隐藏内容

此处内容需要权限查看

  • 普通用户特权:11金币
  • 会员用户特权:免费
  • 永久会员用户特权:免费推荐
会员免费查看