C++
#include <iostream>
#include <string.h>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
const int N = 10010, N1 = 1010;
int site[N1]; //record how many people take exam at site i for a specific date, used in task3
struct Person
{
string id;
int score;
bool operator<(const Person& A) {
if (score != A.score) return score > A.score; //score in decreasing order
else return id < A.id; //ascending alphabetical order;
}
};
vector<Person> all, T, A, B;
struct Site
{
int s;
int num;
bool operator<(const Site& A) {
if (num != A.num) return num > A.num; //sort in decreasing order of person num
return s < A.s; //same person num, sort in ascending order of site number
}
};
vector<Site> date_site;
int n, m;
void task1(string term)
{
vector<Person> aim;
if (term == "A") aim = A;
else if (term == "B") aim = B;
else if (term == "T") aim = T;
if (aim.size() == 0) {
cout << "NA";
}
else {
sort(aim.begin(), aim.end());
for (int i = 0; i < aim.size(); i++ ) {
if (i) cout << endl;
string id = aim[i].id;
int score = aim[i].score;
cout << id << " " << score;
}
}
}
void task2(string term)
{
int total_person = 0, total_score = 0;
for (int i = 0; i < all.size(); i++) {
string id = all[i].id;
int score = all[i].score;
string csite = id.substr(1, 3); //extract current site
if (csite == term) { //it is the target site we want
total_person++ ;
total_score += score;
}
}
if (!total_person) {
cout << "NA";
}
else {
cout << total_person << " " << total_score;
}
}
void task3(string term)
{
memset(site, 0, sizeof(site)); //clear previous result
date_site.clear();
for (int i = 0; i < all.size(); i++ ) {
string id = all[i].id;
int csite = stoi(id.substr(1, 3)); //get current site in int format
string date = id.substr(4, 6); //get date data
if (date == term) { //we find the target date
site[csite]++ ; //csite adds a new person
}
}
for (int k = 0; k <= 999; k++ ) { //traverse all sites
if (site[k]) { //site k has person, add
date_site.push_back({k, site[k]});
}
}
if (date_site.size() == 0) {
cout << "NA";
}
else {
sort(date_site.begin(), date_site.end());
for (int i = 0; i < date_site.size(); i++ ) {
if (i) cout << endl;
int s = date_site[i].s, num = date_site[i].num;
cout << setw(3) << setfill('0') << s << " " << num; //control output format
}
}
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++ ) {
string id; int score;
cin >> id >> score;
all.push_back({id, score});
if (id[0] == 'A') A.push_back({id, score});
else if (id[0] == 'T') T.push_back({id, score});
else if (id[0] == 'B') B.push_back({id, score});
}
for (int round = 1; round <= m; round++ ) {
if (round > 1) cout << endl;
int type; string term;
cin >> type >> term;
cout << "Case " << round << ": " << type << " " << term << endl;
if (type == 1) {
task1(term);
}
else if (type == 2) {
task2(term);
}
else if (type == 3) {
task3(term);
}
}
return 0;
}
请问这道题原题在哪
PAT甲级练习题库里的,你去PAT官网翻到下面真题练习点进去选甲级
或者Acwing上1647题就是