当前位置: 代码迷 >> 综合 >> PAT刷题记录 1025 PAT Ranking (25分)
  详细解决方案

PAT刷题记录 1025 PAT Ranking (25分)

热度:51   发布时间:2024-01-28 00:07:16.0
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;/*总结:用vector的insert来进行合并操作
*/struct Student {string id;int grade;int location_number;int local_rank;int final_rank;
};
vector<Student> stu[105];int now;
void local_rank() {stu[now][0].local_rank = 1;int size = stu[now].size();for (int i = 1; i < size; i++) {if (stu[now][i].grade == stu[now][i - 1].grade)stu[now][i].local_rank = stu[now][i - 1].local_rank;else stu[now][i].local_rank = i + 1;}
}void final_rank() {stu[0][0].final_rank = 1;int size = stu[0].size();for (int i = 1; i < size; i++) {if (stu[0][i].grade == stu[0][i - 1].grade)stu[0][i].final_rank = stu[0][i - 1].final_rank;else stu[0][i].final_rank = i + 1;}
}bool cmp(Student a, Student b) {return a.grade > b.grade;
}bool cmp2(Student a, Student b) {if (a.grade != b.grade)return a.grade > b.grade;else return a.id < b.id;
}int main() {int list_num;cin >> list_num;int cnt = 0;for (int i = 0; i < list_num; i++) {int n;cin >> n;cnt += n;for (int j = 0; j < n; j++) {Student temp;cin >> temp.id>>temp.grade;temp.location_number = i+1;stu[i].push_back(temp);}sort(stu[i].begin(), stu[i].end(), cmp);now = i;local_rank();}//合并rankfor (int i = 1; i < list_num; i++) {stu[0].insert(stu[0].end(), stu[i].begin(), stu[i].end());}sort(stu[0].begin(), stu[0].end(), cmp2);final_rank();printf("%d\n", cnt);for (int i = 0; i < cnt; i++) {cout << stu[0][i].id;printf(" %d %d %d\n", stu[0][i].final_rank, stu[0][i].location_number, stu[0][i].local_rank);}system("pause");return 0;
}