代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
int grade[100010][6]={0};
int full[6]={0};
struct record
{
int id;
int score[6]={0};
int total = 0;
int f[6] = {0};
int flag[6] = {0};//判断每个选手的题目编号是否满分,来规避一个人同时输入多个满分答案
int f2;
int flag2 = 0;//flag2是用来判断这个数是否该输出
int rank;
}stu[10010];
bool cmp(record a,record b)
{
if(a.flag2 != b.flag2) return a.flag2 > b.flag2;
else if(a.total!=b.total) return a.total > b.total;
else if(a.f2 != b.f2) return a.f2 > b.f2;
else return a.id<b.id;
}
//一个题目多次出现满分,只能算一次。。。并且sort的排序不是你臆想的。。。什么的都在cmp的说清楚务必!
int main()
{
int N,K,M,i,id,j,question,sco;
scanf("%d %d %d",&N,&K,&M);
for(i=1;i<=K;i++)
scanf("%d",&full[i]);
for(i=0;i<M;i++)
{
scanf("%d %d %d",&id,&question,&sco);
stu[id].id = id;
if(sco == full[question]) stu[id].f[question]=1;
if(sco == 0 ) {
stu[id].flag[question] = 1;
}
if(sco != -1) stu[id].flag2 = 1;
if(sco == -1) stu[id].flag[question] = 1;
else if(sco>stu[id].score[question])
{
stu[id].score[question] = sco;
}
}
for(i=0;i<=N;i++)
{
stu[i].f2 = stu[i].f[1]+stu[i].f[2]+stu[i].f[3]+stu[i].f[4]+stu[i].f[5];
for(j=1;j<=K;j++)
{
stu[i].total+=stu[i].score[j];
}
}
sort(stu,stu+N+1,cmp);
stu[0].rank = 1;
for(i=0;i<N;i++)
{
if(stu[i].flag2)
{
// && stu[i].total!= -1
if(stu[i].total == stu[i-1].total)
stu[i].rank = stu[i-1].rank;
else stu[i].rank = i+1;
printf("%d %05d %d",stu[i].rank,stu[i].id,stu[i].total);
for(j=1;j<=K;j++)
{
if(stu[i].score[j]!=0 || stu[i].flag[j])
printf(" %d",stu[i].score[j]);
else printf(" -");
}
printf("\n");
}
else if(stu[i].flag2 == 0) break;
}
return 0;
}
更多细节:https://blog.csdn.net/lyw_321/article/details/71143690
我的错误:我一开始的cmp比较函数中没有设置flag2比较的,因为我臆想,sort函数比较之后会把为0的放在我比较的数目后面,而且我的数组是设置很大的,比较的数组长度是设置为用户数,并没有全部长度,所以在没有flag2比较的时候,我输出的不包括总分为零的数!然后把flag比较一波就好了!