首页 > 学院 > 开发设计 > 正文

1025. PAT Ranking (25)

2019-11-11 01:59:21
字体:
来源:转载
供稿:网友

一道水题,vector存储学生,每输入一个地方的学生,放入临时的vector中,并进行排序,设置好地方排名,然后都放入总的一个vector中,最后对总的vector进行sort,注意sort的时候如果成绩一样,按id的顺序排,设置好总排名,输出即可

#include<iostream>#include<vector>#include<string>#include<algorithm>using namespace std;typedef struct student{ string id; int grade; int final_rank; int location_num; int local_rank;}stu;bool compare(stu s1, stu s2){ if (s1.grade > s2.grade || (s1.grade == s2.grade && s1.id.compare(s2.id) < 0)) return true; else return false;}int main(){ vector<stu> all; int N; cin >> N; for (int t = 1;t <= N;t++) { stu temp_stu; vector<stu> temp_vec; int K; cin >> K; for (int i = 0;i < K;i++) { temp_stu.location_num = t; string str; int g; cin >> str >> g; temp_stu.id = str; temp_stu.grade = g; temp_vec.push_back(temp_stu); } sort(temp_vec.begin(), temp_vec.end(), compare); temp_vec[0].local_rank = 1; for (int i = 2;i <= temp_vec.size();i++) if (temp_vec[i - 1].grade == temp_vec[i - 2].grade) temp_vec[i - 1].local_rank = temp_vec[i - 2].local_rank; else temp_vec[i - 1].local_rank = i; all.insert(all.end(), temp_vec.begin(), temp_vec.end()); } sort(all.begin(), all.end(), compare); all[0].final_rank = 1; for (int i = 2;i <= all.size();i++) if (all[i - 1].grade == all[i - 2].grade) all[i - 1].final_rank = all[i - 2].final_rank; else all[i - 1].final_rank = i; cout << all.size() << endl; for (auto x : all) cout << x.id << " " << x.final_rank << " " << x.location_num << " " << x.local_rank << endl;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表