1153 Decode Registration Card of PAT
Posted wsggb123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1153 Decode Registration Card of PAT相关的知识,希望对你有一定的参考价值。
这道题一开始一直超时,后来看了大神的发现要用unordered_map,然后还是不行,把cout改成printf,居然过了。。。时间真的掐得紧
#include <iostream> #include <stdlib.h> #include <string> #include<algorithm> #include<vector> #include<cmath> #include<map> #include <unordered_map> using namespace std; struct stu { string cnum; int score; }; struct site { int num; string s; }; bool cmp1(stu x, stu y) { if (x.score != y.score) { return x.score > y.score; } else { return x.cnum < y.cnum; } } bool cmp2(site x, site y) { if (x.num != y.num) { return x.num > y.num; } else { return x.s < y.s; } } int main(){ int m, n; cin >> n >> m; vector<stu> arr; for (int i = 0; i < n; i++) { stu s; cin >> s.cnum >> s.score; arr.push_back(s); } for (int i = 0; i < m; i++) { int type; cin >> type; if (type == 1) { char level; cin >> level; int num = 0; sort(arr.begin(), arr.end(), cmp1); cout << "Case" << ‘ ‘ << i + 1 << ": " << type << ‘ ‘ << level<<endl; for (int j = 0; j < arr.size(); j++) { if (arr[j].cnum[0] == level) { printf("%s %d ", arr[j].cnum.c_str(), arr[j].score); num++; } } if (num == 0) { cout << "NA" << endl; } } if (type == 2) { string site; cin >> site; int num = 0, total = 0; for (int j = 0; j < arr.size(); j++) { if (arr[j].cnum.substr(1, 3) == site) { num++; total += arr[j].score; } } cout << "Case" << ‘ ‘ << i + 1 << ": " << type << ‘ ‘ << site<<endl; if (num == 0) { cout << "NA" << endl; } else { cout << num << ‘ ‘ << total << endl; } } if (type == 3) { string date; cin >> date; unordered_map<string, int> m1; vector<site> arr1; for (int j = 0; j < arr.size(); j++) { if (arr[j].cnum.substr(4, 6) == date) { m1[arr[j].cnum.substr(1, 3)]++; } } for (auto it : m1) { site temp; temp.s = it.first; temp.num = it.second; arr1.push_back(temp); } sort(arr1.begin(), arr1.end(), cmp2); cout << "Case" << ‘ ‘ << i + 1 << ": " << type << ‘ ‘ << date << endl; if (arr1.size() == 0) { cout << "NA" << endl; } else { for (int j = 0; j < arr1.size(); j++) { printf("%s %d ", arr1[j].s.c_str(), arr1[j].num); } } } } system("pause"); };
以上是关于1153 Decode Registration Card of PAT的主要内容,如果未能解决你的问题,请参考以下文章
PAT Advanced 1153 Decode Registration Card of PAT (25 分)
1153.Decode Registration Card of PAT(unordered_map)
1153 Decode Registration Card of PAT (25 分)难度: 一般 / 知识点: 排序 模拟
PAT A1153 Decode Registration Card of PAT [硬核模拟]