PAT (Advanced Level) Practice 排序题ios
#include<algorithm> using namespace std; void sort (first, last, cmp); // first 首元素地址;last 尾元素地址下一个地址;cmp 比较函数
struct Student { char name[10]; char id[10]; int score; int rank; }stu[10000];
分数不一样分数高在前,分数相同姓名字典序小在前算法
bool cmp(Student a, Student b){ return a.score != b.score ? a.score > b.score : strcmp(a.name, b.name) < 0; }
分数不一样排名不一样,分数相同排名相同占用同一个排位编程
stu[0].rank = 1; for (int i = 1; i < n; i++){ if (stu[i].score == stu[i-1].score) stu[i].rank = stu[i-1].rank; else stu[i].rank = i + 1; }
int rank = 1; for (int i = 0; i < n; i++) if (i > 0 && stu[i].score != stu[i-1].score) rank = i + 1;
#include<unordered_map> #include<vector> #include<iostream> #include<algorithm> using namespace std; struct Student{ int id, best; int score[4], rank[4]; }; int sortby = -1; bool cmp (Student a, Student b) { return a.score[sortby] > b.score[sortby];}; int main() { int n, m, id; scanf("%d%d",&n,&m); vector<Student> stu(n+1); for (int i = 0; i < n; i++){ scanf("%d%d%d%d",&stu[i].id,&stu[i].score[1],&stu[i].score[2],&stu[i].score[3]); stu[i].score[0] = (stu[i].score[1] + stu[i].score[2] + stu[i].score[3]) / 3; } for (sortby = 0; sortby < 4; sortby++){ sort(stu.begin(),stu.end(),cmp); stu[0].rank[sortby] = 1; for (int i = 1; i < n; i++){ if (stu[i].score[sortby] != stu[i-1].score[sortby]) stu[i].rank[sortby] = i+1; else stu[i].rank[sortby] = stu[i-1].rank[sortby]; } } unordered_map<int, int> index; for (int i = 0; i < n; i++){ index.insert(pair<int,int>(stu[i].id,i)); stu[i].best = 0; int minr = stu[i].rank[0]; for (int j = 1; j < 4; j++){ if (stu[i].rank[j] < minr){ stu[i].best = j; minr = stu[i].rank[j]; } } } char symbol[] = "ACME"; for (int i = 0; i < m; i++){ scanf("%d",&id); if (index.find(id) == index.end()) printf("N/A\n"); else{ int best = stu[index[id]].best; printf("%d %c\n",stu[index[id]].rank[best], symbol[best]); } } return 0; }
#include<unordered_map> #include<vector> #include<iostream> #include<algorithm> using namespace std; struct Student{ string id; int c, math, eng; double avg; int r_c, r_math, r_eng, r_avg; Student (){} Student (string _id, int _c, int _math, int _eng): id(_id), c(_c), math(_math), eng(_eng){ avg = (c + math + eng) / 3.0; } }; bool cmp_c (Student a, Student b){ return a.c > b.c;} bool cmp_math (Student a, Student b){ return a.math > b.math;} bool cmp_eng (Student a, Student b){ return a.eng > b.eng;} bool cmp_avg (Student a, Student b){ return a.avg > b.avg;} int main() { int n, m, c, math, eng; string id; cin >> n >> m; vector<Student> stu; unordered_map<string, int> index; for (int i = 0; i < n; i++){ cin >> id >> c >> math >> eng; stu.push_back(Student(id,c,math,eng)); } sort(stu.begin(),stu.end(),cmp_c); stu[0].r_c = 1; for (int i = 1; i < n; i++){ if (stu[i].c == stu[i-1].c) stu[i].r_c = stu[i-1].r_c; else stu[i].r_c = i+1; } sort(stu.begin(),stu.end(),cmp_math); stu[0].r_math = 1; for (int i = 1; i < n; i++){ if (stu[i].math == stu[i-1].math) stu[i].r_math = stu[i-1].r_math; else stu[i].r_math = i+1; } sort(stu.begin(),stu.end(),cmp_eng); stu[0].r_eng = 1; for (int i = 1; i < n; i++){ if (stu[i].eng == stu[i-1].eng) stu[i].r_eng = stu[i-1].r_eng; else stu[i].r_eng = i+1; } sort(stu.begin(),stu.end(),cmp_avg); stu[0].r_avg = 1; index.insert(pair<string,int>(stu[0].id,0)); for (int i = 1; i < n; i++){ if (stu[i].avg == stu[i-1].avg) stu[i].r_avg = stu[i-1].r_avg; else stu[i].r_avg = i+1; index.insert(pair<string,int>(stu[i].id,i)); } for (int i = 0; i < m; i++){ cin >> id; if (index.find(id) == index.end()) cout << "N/A\n"; else{ int min_r = min(min(min(stu[index[id]].r_c,stu[index[id]].r_math),stu[index[id]].r_eng),stu[index[id]].r_avg); cout << min_r << " "; if (min_r == stu[index[id]].r_avg) cout << "A\n"; else if (min_r == stu[index[id]].r_c) cout << "C\n"; else if (min_r == stu[index[id]].r_math) cout << "M\n"; else cout << "E\n"; } } return 0; }
#include<iostream> #include<vector> #include<algorithm> using namespace std; struct Student{ string name, id; int grade; }; bool cmp(Student a, Student b){ return a.grade > b.grade; } int main() { int n, grade, min, max; scanf("%d", &n); string name, id; vector<Student> list; for (int i = 0; i < n; i++){ cin >> name >> id >> grade; list.push_back({name, id, grade}); } sort(list.begin(), list.end(), cmp); scanf("%d%d", &min, &max); bool empty = true; for (int i = 0; i < list.size(); i++){ if (list[i].grade < min) break; if (list[i].grade > max) continue; if (empty) empty = false; cout << list[i].name << " " << list[i].id << endl; } if (empty) printf("NONE\n"); return 0; }
map.lower_bound()
和 map.upper_bound()
输出要求信息#include<iostream> #include<map> using namespace std; int main(){ map<int,string,greater<int>> records; int n, grade, min, max; string name, ID; scanf("%d", &n); for (int i = 0; i < n; i++) { cin >> name >> ID >> grade; records.insert(pair<int,string>(grade,name + " " + ID)); } scanf("%d%d", &min, &max); if (records.lower_bound(max) == records.upper_bound(min)) cout << "NONE" << endl; else for (auto it = records.lower_bound(max); it != records.upper_bound(min); ++it) cout << it->second << endl; return 0; }
#include<iostream> #include<unordered_map> #include<vector> #include<algorithm> #include<cmath> using namespace std; struct Student{ string id; int p, mid, final, grade; }; bool cmp(Student a, Student b) { return a.grade != b.grade ? a.grade > b.grade : a.id < b.id; } int main() { int p, m, n, score; scanf("%d%d%d", &p, &m, &n); unordered_map<string,int> program, mid, final; string id; for (int i = 0; i < p; i++){ cin >> id >> score; if (score <= 900) program.insert({id,score}); } for (int i = 0; i < m; i++){ cin >> id >> score; if (score <= 100) mid.insert({id,score}); } for (int i = 0; i < n; i++){ cin >> id >> score; if (score <= 100) final.insert({id,score}); } vector<Student> list; for (auto it: program){ string id = it.first; if (it.second < 200 || final.find(id) == final.end()) continue; if (mid.find(id) != mid.end() && mid[id] > final[id]) score = round(mid[id]*0.4 + final[id]*0.6); else score = final[id]; if (score >= 60) list.push_back({id,it.second,mid.find(id)==mid.end()?-1:mid[id],final[id],score}); } sort(list.begin(), list.end(), cmp); for (auto it: list) printf("%s %d %d %d %d\n", it.id.c_str(), it.p,it.mid,it.final,it.grade); return 0; }
#include<iostream> #include<unordered_map> #include<vector> #include<algorithm> using namespace std; struct School{ string ID; int TWS, Ns; }; bool cmp (School a, School b){ return a.TWS != b.TWS ? a.TWS > b.TWS : a.Ns != b.Ns ? a.Ns < b.Ns : a.ID < b.ID; } int main() { unordered_map<string,int> ns; unordered_map<string,double> tws; string id, school; int n, score, rank = 1; scanf("%d\n", &n); for (int i = 0; i < n; i++){ cin >> id >> score >> school; transform(school.begin(), school.end(), school.begin(), ::tolower); ns[school]++; if (id[0] == 'T') tws[school] += score * 1.5; else if (id[0] == 'A') tws[school] += score; else tws[school] += score / 1.5; } vector<School> ranklist; for (auto it = ns.begin(); it != ns.end(); it++){ ranklist.push_back({it->first, (int)tws[it->first], it->second}); } sort(ranklist.begin(), ranklist.end(), cmp); printf("%d\n%d %s %d %d\n", ranklist.size(), rank, (ranklist[0].ID).c_str(), ranklist[0].TWS, ranklist[0].Ns); for (int i = 1; i < ranklist.size(); i++){ if (ranklist[i].TWS != ranklist[i-1].TWS) rank = i + 1; printf("%d %s %d %d\n", rank, (ranklist[i].ID).c_str(), ranklist[i].TWS, ranklist[i].Ns); } return 0; }
transform(word.begin(),word.end(),new_word.begin(),op);
要转化为大写,
op = ::toupper
,因 toupper 是在全局命名空间而非 std 中,因此要加 ::
#include<iostream> #include<vector> #include<algorithm> using namespace std; struct CardNumber{ string cardnum, date; char level; int site, testeenum, score; CardNumber(string _cardnum, int _score): cardnum(_cardnum), score(_score){ level = _cardnum[0]; site = stoi(_cardnum.substr(1,3)); date = _cardnum.substr(4,6); testeenum = stoi(_cardnum.substr(10,3)); } }; struct SiteNum{ int site, num; SiteNum(int _site, int _num): site(_site), num(_num) {} }; bool cmp_card(CardNumber a, CardNumber b){ return a.score != b.score ? a.score > b.score : a.cardnum < b.cardnum; } bool cmp_site(SiteNum a, SiteNum b){ return a.num != b.num ? a.num > b.num : a.site < b.site; } int main() { int n, m, score, site_tnum[1000] = {0}, site_tscore[1000] = {0}; string cardnum; scanf("%d%d", &n, &m); vector<CardNumber> info; for (int i = 0; i < n; i++){ cin >> cardnum >> score; info.push_back(CardNumber(cardnum,score)); site_tnum[info[i].site]++; site_tscore[info[i].site] += score; } sort(info.begin(), info.end(), cmp_card); int type, site; char level; string date; for (int i = 0; i < m; i++){ scanf("%d ", &type); bool isempty = true; switch(type){ case 1: scanf("%c",&level); printf("Case %d: %d %c\n", i+1, type, level); for (int j = 0; j < n; j++){ if (info[j].level == level){ isempty = false; cout << info[j].cardnum << " " << info[j].score << endl; } } if (isempty) printf("NA\n"); break; case 2: scanf("%d",&site); printf("Case %d: %d %d\n", i+1, type, site); if (!site_tnum[site]) printf("NA\n"); else printf("%d %d\n", site_tnum[site], site_tscore[site]); break; case 3: cin >> date; cout << "Case " << i+1 << ": " << type << " " << date << endl; int datesitenum[1000] = {0}; vector<SiteNum> dsn; for (int j = 0; j < n; j++){ if (info[j].date == date){ isempty = false; datesitenum[info[j].site]++; } } if (isempty) printf("NA\n"); else{ for (int j = 0; j < 1000; j++) if (datesitenum[j]) dsn.push_back(SiteNum(j,datesitenum[j])); sort(dsn.begin(),dsn.end(),cmp_site); for (int j = 0; j < dsn.size(); j++) printf("%d %d\n", dsn[j].site, dsn[j].num); } break; } } return 0; }