日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

PAT甲级1153 Decode Registration Card of PAT :[C++题解]结构体、哈希表、排序、类似数据库查询题、好题

發(fā)布時(shí)間:2025/4/5 c/c++ 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PAT甲级1153 Decode Registration Card of PAT :[C++题解]结构体、哈希表、排序、类似数据库查询题、好题 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

    • 題目分析
    • 題目來源

題目分析



來源:acwing

分析:

本題是三種不同的詢問,難道要寫三個(gè)結(jié)構(gòu)體?想了想,就寫了一個(gè)結(jié)構(gòu)體,參賽人結(jié)構(gòu)體,內(nèi)容包括考號(hào)id和成績(jī)grade,然后開結(jié)構(gòu)體數(shù)組p[N]讀入信息。

查詢信息就基于該結(jié)構(gòu)體數(shù)組。

  • 查詢1:按照級(jí)別排序。這里直接遍歷數(shù)組P[N],找到對(duì)應(yīng)級(jí)別的考生,放進(jìn)vector排序即可。簡(jiǎn)單。
  • 查詢2:查詢某一考場(chǎng)人數(shù)和總分。這里直接遍歷數(shù)組P[N],找到對(duì)應(yīng)考場(chǎng)數(shù)值累加即可。簡(jiǎn)單。
  • 查詢3:查詢某一日期的考生,分考場(chǎng)輸出人數(shù)。這個(gè)稍稍有點(diǎn)復(fù)雜,需要查詢?nèi)掌诤?#xff0c;再建一個(gè)哈希表,統(tǒng)計(jì)考場(chǎng)的人數(shù)。然后放入vector進(jìn)行雙關(guān)鍵字排序。pair<int,string> 存的是<人數(shù),考場(chǎng)>。

ac代碼

#include<bits/stdc++.h> using namespace std; const int N = 1e4+10; struct Person{string id;int grade;bool operator<(const Person& t)const{if(grade != t.grade) return grade > t.grade;return id < t.id;}}p[N];int main(){int n, m;cin >> n >> m;for(int i = 0; i< n; i++) cin>> p[i].id >> p[i].grade;for(int i = 1; i<= m; i++){string t ,c;cin >> t>> c;printf("Case %d: %s %s\n",i,t.c_str(),c.c_str());if(t == "1"){vector<Person> persons;for(int i = 0; i<n; i++){if(p[i].id[0]== c[0] )persons.push_back(p[i]);}if(persons.empty()) puts("NA");else{sort(persons.begin(),persons.end());for(auto p :persons) printf("%s %d\n",p.id.c_str(),p.grade);}}else if(t=="2"){int cnt = 0, sum = 0;for(int i = 0; i< n; i++)if(p[i].id.substr(1,3) ==c) cnt++,sum += p[i].grade;if(!cnt) puts("NA");else printf("%d %d\n",cnt ,sum);}else if(t=="3"){unordered_map<string,int> mp;for(int i =0; i<n; i++){string date = p[i].id.substr(4,6);string sch =p[i].id.substr(1,3);if(date == c) mp[sch]++;}vector<pair<int ,string>> rooms;// 這里數(shù)值添-號(hào),直接sort就相當(dāng)于從大到小排序啦!!!//就不用寫什么greater啦。for(auto m:mp) rooms.push_back({-m.second,m.first});sort(rooms.begin(),rooms.end());if(rooms.empty()) puts("NA");else{for(auto room:rooms)printf("%s %d\n",room.second.c_str(),-room.first);}}} }

題目來源

PAT甲級(jí)1153 Decode Registration Card of PAT
https://www.acwing.com/problem/content/1649/

總結(jié)

以上是生活随笔為你收集整理的PAT甲级1153 Decode Registration Card of PAT :[C++题解]结构体、哈希表、排序、类似数据库查询题、好题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。