本文主要是介绍1025 PAT Ranking (25 分)解题思路,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805474338127872
思路
直接开辟30000个结构体数组(N<=100, K <= 300). 在每次输入完K个考生信息(ID, location_number, score)
的后,对这局部的K的结构体进行sort排序,排序算法使用自定义的cmp函数(见下面代码),cmp使用了双关键字排序,如果分数相同,则按照ID进行升序排列。拍完这K个结构体后,再次遍历一遍,完善好信息(local_rank)
最后,对所有的结构体进行总的排序,算出总的排名,然后按照要求输出即可。
代码
#include <iostream>
#include <algorithm>
using namespace std;struct testee {string ID;int location_number;int local_rank;int score;
}testees[30000];bool cmp(struct testee testee1, struct testee testee2){if(testee1.score == testee2.score)return testee1.ID < testee2.ID;return testee1.score > testee2.score;
}int main(){int N, K;int count = 0; //测试者人数的计数int rank = 1; //总排名cin >> N;for(int i=0; i<N; i++){cin >> K;for(int j=0; j<K; j++){cin >> testees[count].ID >> testees[count].score;testees[count].location_number = i+1;count ++;}sort(testees+count-K, testees+count, cmp);for(int m=count-K; m<=count-1; m++){testees[m].local_rank = m - count + K + 1;}for(int m=count-K; m<=count-2; m++){if(testees[m+1].score == testees[m].score)testees[m+1].local_rank = testees[m].local_rank;}}sort(testees, testees+count, cmp);cout << count << endl;cout << testees[0].ID << ' ' << rank << ' ' << testees[0].location_number << ' ' << testees[0].local_rank << endl;for(int i=1; i<count; i++){if(testees[i].score < testees[i-1].score)rank = i+1;cout << testees[i].ID << ' ' << rank << ' ' << testees[i].location_number << ' ' << testees[i].local_rank << endl;}return 0;
}
这篇关于1025 PAT Ranking (25 分)解题思路的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!