6 Dokumente drucken | Huawei zum Computertest

Link zur ursprünglichen Frage:[Volle Punktzahl][Huawei OD Machine Test Real Questions 2023 JAVA] Datei drucken_Ruo Bodous Blog-CSDN-Blog

Ursprünglich dachte ich, ich müsste Map verwenden, aber nachdem ich die Syntax überprüft hatte, stellte ich fest, dass ich zum Speichern von Triplett-Arrays keinen Vektor verwenden musste, sondern nur zwei Paare verschachteln musste.

#include <iostream>
#include <map>

using namespace std;

int main()
{
    map<int, int> map_a;
    map_a[1] = 1;
    map_a[2] = 2;
    map_a[5] = 3;
    map_a[4] = 4;
    for (auto it = map_a.begin(); it != map_a.end(); it++)
        cout << it->first << ':' << it->second << '\n';
    return 0;
}

Speichern Sie einfach das Drei-Yuan-Paar im Vektor.

Ich habe den Test zweimal gemacht und es gab kein Problem. Ich bin so großartig, hahaha

eingeben:

7
IN 1 1
IN 1 2
IN 1 3
IN 2 1
OUT 1
OUT 2
OUT 2

Ausgabe

3  4  null

5
IN 1 1
IN 1 3
IN 1 1
IN 1 3
OUT 1

Ausgabe

2

Code: 

#include <iostream>
#include <algorithm>

#define x first
#define y second

using namespace std;

const int N = 1010;
typedef pair<int, pair<int, int>> PII;

int m;
vector<PII> alls;

int main()
{
    scanf("%d", &m);
    int count = 1;//文件的编号
    
    while(m -- ) {
        string op;
        
        cin >> op;
        int p, n;
        bool flag = false;
        
        if(op == "IN") 
        {
            scanf("%d%d", &p, &n);//打印机编号p, 优先级n
            alls.push_back({n, {count, p}});
            count ++ ;
            
            //sort(alls.rbegin(), alls.rend());//按照n排序
            sort(alls.rbegin(), alls.rend(), [](const PII &a, const PII &b){
                return a.x < b.x; // 先按照优先级n降序, 再按照插入顺序count升序
            });
            
            // for(auto e : alls) 
            // {
            //     cout << e.x << ' '<< e.y.x << ' ' << e.y.y << endl;
            // } cout << endl;
        } 
        else if(op == "OUT")
        {
            scanf("%d", &p);
            
            int len = alls.size();
            //遍历输出结果
            for(int i = 0; i < len; i ++ ) 
            {
                //如果有结果 输出结果, 在数组中删除该数 flag=true, 退出循环
                if(alls[i].y.y == p) 
                {
                    cout << alls[i].y.x << endl;
                    auto iter = alls.erase(alls.begin() + i);//删除元素

                    flag = true;
                    break;
                } 
            }
            //如果遍历完都没有结果, flag还是false 输出NULL
            if(flag == false) cout << "NULL" << endl;
            flag = false;
        }
    }
    return 0;
}

おすすめ

転載: blog.csdn.net/weixin_65293439/article/details/129878422
おすすめ