\quad 排序题,但是比较繁杂,需要足够细心。需要注意的是总分相同的排名要一样,还有注意一点就是在c++中printf,scanf和cin,cout不能混用,我就是混用后本地测试没问题,但上传去评测就会导致出错。程序如下:
#include<bits/stdc++.h>
using namespace std;
struct Stu
{
int ID=-1;
int allscore = 0;
int score[5] = {-2, -2, -2, -2, -2};
int wellSolve = 0;
int rank = 0;
bool flag;
}stu[10001];
bool cmp(const Stu &s1, const Stu &s2)
{
if(s1.allscore!=s2.allscore) return s1.allscore>s2.allscore;
else if(s1.wellSolve!=s2.wellSolve) return s1.wellSolve>s2.wellSolve;
else return s1.ID<s2.ID;
}
int main()
{
int N, pNum, subNum;
scanf("%d%d%d", &N, &pNum, &subNum);
int score[pNum];
for (int i = 0; i < pNum; ++i) {
scanf("%d", &score[i]);
}
int ID, problem, sco;
for (int i = 1; i < subNum+1; ++i) {
scanf("%d%d%d", &ID, &problem, &sco);
stu[ID].ID = ID;
if(sco>stu[ID].score[problem-1]) stu[ID].score[problem-1] = sco;
}
for (int i = 1; i < N+1; ++i) {
bool flag = false;
for (int j = 0; j < pNum; ++j) {
if(stu[i].score[j]>=0) {
stu[i].allscore += stu[i].score[j];
flag = true;
}
if(stu[i].score[j]==score[j]) stu[i].wellSolve += 1;
}
stu[i].flag = flag;
}
sort(stu+1, stu+N+1, cmp);
// 计算排名,总分相同的排名一样
stu[1].rank = 1;
for (int i = 2; i < N+1; ++i)
{
if(stu[i].allscore==stu[i-1].allscore) stu[i].rank = stu[i-1].rank;
else stu[i].rank = i;
}
for (int i = 1; i < N + 1; ++i) {
if(!stu[i].flag) continue;
printf("%d %05d %d", stu[i].rank, stu[i].ID, stu[i].allscore);
for (int j = 0; j < pNum; ++j) {
if(stu[i].score[j]>=0) printf(" %d", stu[i].score[j]);
else if(stu[i].score[j]==-2) printf(" -");
else if(stu[i].score[j]==-1) printf(" 0");
}
cout << endl;
}
return 0;
}