赞
踩
原题链接:【满分】【华为OD机试真题2023 JAVA】 打印文件_若博豆的博客-CSDN博客
本来以为需要用map, 还复习了一下语法, 原来不需要, 用vector存三元数组, 两个pair嵌套就可以
- #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;
- }
三元的pair 存到vector就可以了
测了两次没问题, 我太牛了哈哈哈
输入:
7
IN 1 1
IN 1 2
IN 1 3
IN 2 1
OUT 1
OUT 2
OUT 2
输出
3 4 null
5
IN 1 1
IN 1 3
IN 1 1
IN 1 3
OUT 1
输出
2
代码:
- #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;
- }
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。