PAT1025

来源:互联网 发布:赚钱宝映射哪些端口号 编辑:程序博客网 时间:2024/05/29 08:19
#include<cstdio>#include<algorithm>#include<cstring>typedef struct pat{char regist[15];int score;int loc = 0;//标注考场int loc_rank = 0;//标注考场内部排名int total_rank = 0;}pat;//除了最大的全体排序外 其他的排序都应该内部建立在struct里面用参数来排名bool cmp(pat A, pat B)//所有这类排序题目核心在于建立cmp函数内的排序机制{if (A.score != B.score)return A.score > B.score;else return (std::strcmp(A.regist, B.regist))<0;}int main(){pat student[30010];//存放所有的数据int N;//the number of test locationsscanf_s("%d", &N);int index = 0;//总的下标for (int i = 0; i < N; i++){int K;//the number of testees in one locationscanf_s("%d", &K);for (int j = 0; j < K; j++){scanf_s("%s %d", student[index].regist, &student[index].score);student[index].loc= i+1;index++;}//同一个考场内部的排序std::sort(student+index-K, student+index, cmp);//在完成一个考场的输入后,//现在这个考场对应的下标应该是student[index-K], student[index]//别忘了考虑同分的排名情况student[index - K].loc_rank = 1;for (int m = 1; m < K; m++){if (student[index - K + m].score == student[index - K + m - 1].score)student[index - K + m].loc_rank = student[index - K + m - 1].loc_rank;elsestudent[index - K + m].loc_rank = m + 1;}}//finish inputprintf("%d\n", index);std::sort(student, student + index, cmp);student[0].total_rank = 1;printf("%13s %d %d %d\n", student[0].regist, student[0].total_rank, student[0].loc, student[0].loc_rank);for (int r = 1; r < index; r++){if (student[r].score == student[r - 1].score)student[r].total_rank = student[r - 1].total_rank;elsestudent[r].total_rank = r + 1;printf("%13s %d %d %d\n", student[r].regist, student[r].total_rank, student[r].loc, student[r].loc_rank);}}

原创粉丝点击