这题是排序题,简单,但坑在细节。
题目说“For those who has never submitted any solution that can pass the compiler, or has never submitted any solution, they must NOT be shown on the ranklist.”存在一种情况:用户pass the compiler 但是得分为0,还是得输出。所以不能根据总得分是否为0判断该不该被输出。
有些细节的判断条件需要仔细理清。
代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<math.h>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
#include <cstdlib>
using namespace std;
struct user{
string id;
int test[5]={-2,-2,-2,-2,-2};
int total=0;
int numget;
int perfect=0;
int rank;
int flag=0;
}peo[10010];
bool cmp(user a,user b){
if(a.total!=b.total) return a.total>b.total;
else if(a.perfect!=b.perfect) return a.perfect>b.perfect;
else return a.id<b.id;
}
int main(){
int numid,numq,numsub;
cin>>numid>>numq>>numsub;
int score[5]={0};
for(int i=0;i<numq;i++){
cin>>score[i];
}
for(int i=0;i<numsub;i++){
string idstr;
int qes,fenshu;
cin>>idstr>>qes>>fenshu;
int idint=stoi(idstr)-1;
peo[idint].id=idstr;
if(peo[idint].test[qes-1]<fenshu){
if(fenshu!=-1){
if(peo[idint].test[qes-1]>=0){
peo[idint].total=peo[idint].total-peo[idint].test[qes-1]+fenshu;
}
else peo[idint].total=peo[idint].total+fenshu;
peo[idint].flag=1;
}
peo[idint].test[qes-1]=fenshu;
if(peo[idint].test[qes-1]==score[qes-1]){
peo[idint].perfect++;
}
}
}
sort(peo,peo+numid,cmp);
peo[0].rank=1;
for(int i=1;i<numid;i++){
if(peo[i].total!=peo[i-1].total) peo[i].rank=i+1;
else peo[i].rank=peo[i-1].rank;
}
for(int i=0;i<numid;i++){
if(peo[i].total>0||peo[i].flag==1){
cout<<peo[i].rank<<" "<<peo[i].id<<" "<<peo[i].total;
for(int j=0;j<numq;j++){
if(peo[i].test[j]>=0) cout<<" "<<peo[i].test[j];
else if(peo[i].test[j]==-1) cout<<" 0";
else cout<<" -";
}
cout<<endl;
}
}
}
最后一点小贴士:
c++ string转int 用stoi()
函数,头文件#include<string>
.