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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

PAT甲级1141 PAT Ranking of Institutions :[C++题解]结构体、排序、哈希表、结构体构造函数、结构体内写函数、排名

發(fā)布時間:2025/4/5 c/c++ 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PAT甲级1141 PAT Ranking of Institutions :[C++题解]结构体、排序、哈希表、结构体构造函数、结构体内写函数、排名 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

    • 題目分析
    • 題目來源

題目分析



來源:acwing

分析:和下面這題是一道題:
PAT甲級1137 Final Grading:[C++題解]結(jié)構體、排序、哈希表、結(jié)構體構造函數(shù)、結(jié)構體內(nèi)寫函數(shù)

排名得記錄一下,做過幾道類似的題目:學校首先按加權總分排行。如有并列,則應對應相同的排名,并按考生人數(shù)升序輸出。這種處理方式是設一個變量rank,如果分數(shù)不一樣的話,排名rank就等于前面的人數(shù)+1;如果分數(shù)一樣的話,rank不變。

樣例: 5 1 cmu 192 2 1 au 192 3 3 pku 100 1 4 hypu 81 2 4 lanx 81 2 代碼:int rank =1;for(int i = 0; i<schools.size(); i++){auto a = schools[i];if(i== 0 || a.score != schools[i-1].score) rank = i+1;printf("%d %s %d %d\n",rank,a.id.c_str(),a.score,a.cnt);}

ac代碼

#include<bits/stdc++.h> using namespace std; const int N = 1e5+10;int n; struct School{string id;int b,a,t;int cnt;int score; //默認構造函數(shù)School(): b(0),a(0),t(0),cnt(0),score(0) {}//求總分數(shù),取整(int)不是四舍五入void calc(){score = (int)(b/1.5 + a + t*1.5);}//排序:重載小于號bool operator<(const School& s)const{if(score != s.score) return score > s.score;else if(cnt != s.cnt) return cnt < s.cnt;return id < s.id;}};//變成小寫 string change(string a){string res;for(auto s :a)s=tolower(s),res +=s;return res; }int main(){cin >>n ;//hash表unordered_map<string,School> hash1;for(int i = 0; i<n ; i++){string id,sch;int score;cin >> id >> score >> sch;sch=change(sch);if(id[0]=='B') hash1[sch].b += score;if(id[0]=='A') hash1[sch].a += score;if(id[0]=='T') hash1[sch].t += score;hash1[sch].cnt ++;hash1[sch].id = sch;}vector<School> schools;for(auto sch:hash1) {auto s = sch.second;s.calc(); //求總分schools.push_back(s);//放進vector}sort(schools.begin(),schools.end());cout<< schools.size()<<endl;//排名int rank =1;for(int i = 0; i<schools.size(); i++){auto a = schools[i];if(i== 0 || a.score != schools[i-1].score) rank = i+1;printf("%d %s %d %d\n",rank,a.id.c_str(),a.score,a.cnt);}}

題目來源

PAT甲級1141 PAT Ranking of Institutions
https://www.acwing.com/problem/content/1636/

總結(jié)

以上是生活随笔為你收集整理的PAT甲级1141 PAT Ranking of Institutions :[C++题解]结构体、排序、哈希表、结构体构造函数、结构体内写函数、排名的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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