[PAT] A1012 The Best Rank

tech2022-09-02  117

(排序;排名的实现)

题目大意

现已知n个考生的3门分数,平均分可以按照这三门算出来。然后分别对这四个分数从高到低排序,这样对每个考生来说有4个排名。m个查询,对于每一个学生id,输出当前id学生的最好的排名和它对应的分数,如果名次相同,按照A>C>M>E的顺序输出。如果当前id不存在,输出N/A。 题目链接

思路

将输入数据存好,用结构体(参数为id、分数和排名);输入时顺便计算平均分(这里因为不涉及到具体数值,可以直接用总分;若是平均分记得四舍五入)。 用sort按分数排序从大到小排序。 计算排名的方法:第一项的排名是1,其余的循环一遍,若当前分数与上一个相同,当前排名就等于上一个的排名;否则排名等于下标i+1。 按照ACME的比较被询问的各个人的排名,若大于才交换,可以保证相同的时候是按照ACME的优先级输出的。

化简

1、用一个结构体存储学生的id(题目说是6位数字,可以直接存成整型)、四门成绩、四门排名、最好的排名的对应的科目下标

struct node { int id, best; int score[4], rank[4]; }stu[2002];

2、存储的时候就按照ACME的顺序存储可以简化程序逻辑 3、用exist数组保存当前id是否存在,这个id对应的stu结构体的下标是多少。用i+1可以保证为0的都是不存在的可以直接输出N/A,其余不为0的保存的值是对应的结构体index + 1的值(在输出时结构体下标记得减一)

AC代码(未化简)

#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include <cstdio> #include<algorithm> #include <cstring> using namespace std; #define N 2000 struct A { int score, rank; string id; }aaa[N]; struct C { int score, rank; string id; }ccc[N]; struct M { int score, rank; string id; }mmm[N]; struct E { int score, rank; string id; }eee[N]; bool cmpa(A a, A b) { return a.score > b.score; } bool cmpc(C a, C b) { return a.score > b.score; } bool cmpm(M a, M b) { return a.score > b.score; } bool cmpe(E a, E b) { return a.score > b.score; } int main() { int n, m, i, j; scanf("%d%d", &n, &m); for (i = 0;i < n;i++) { string tid; cin >> tid; scanf("%d", &ccc[i].score); scanf("%d", &mmm[i].score); scanf("%d", &eee[i].score); aaa[i].score = ccc[i].score + mmm[i].score + eee[i].score; aaa[i].id = ccc[i].id = mmm[i].id = eee[i].id = tid; } sort(aaa, aaa + n, cmpa); sort(mmm, mmm + n, cmpm); sort(ccc, ccc + n, cmpc); sort(eee, eee + n, cmpe); aaa[0].rank = mmm[0].rank = ccc[0].rank = eee[0].rank = 1; for (i = 1;i < n;i++) { if (aaa[i].score == aaa[i - 1].score) aaa[i].rank = aaa[i - 1].rank; else aaa[i].rank = i + 1; if (mmm[i].score == mmm[i - 1].score) mmm[i].rank = mmm[i - 1].rank; else mmm[i].rank = i + 1; if (ccc[i].score == ccc[i - 1].score) ccc[i].rank = ccc[i - 1].rank; else ccc[i].rank = i + 1; if (eee[i].score == eee[i - 1].score) eee[i].rank = eee[i - 1].rank; else eee[i].rank = i + 1; } for (i = 0;i < m;i++) { string checkid; cin >> checkid; int bestrank = n + 1; char bestcourse; bool flag = false; for (j = 0;j < n;j++) { if (aaa[j].id == checkid) { flag = true; bestrank = aaa[j].rank; bestcourse = 'A'; break; } } for (j = 0;j < n;j++) { if (ccc[j].id == checkid) { flag = true; if (bestrank > ccc[j].rank) { bestrank = ccc[j].rank; bestcourse = 'C'; } break; } } for (j = 0;j < n;j++) { if (mmm[j].id == checkid) { flag = true; if (bestrank > mmm[j].rank) { bestrank = mmm[j].rank; bestcourse = 'M'; } break; } } for (j = 0;j < n;j++) { if (eee[j].id == checkid) { flag = true; if (bestrank > eee[j].rank) { bestrank = eee[j].rank; bestcourse = 'E'; } break; } } if (flag == false)printf("N/A\n"); else printf("%d %c\n", bestrank, bestcourse); } return 0; }
最新回复(0)