PAT:1075. PAT Judge (25) AC

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

struct Student
{
  int ID;
  int score[6];
  int perfect;
  int sum;
  int rank;
  bool tag;
}S[10066];

int p[6];   //1-5存题号1-5小题的分值
int mp[10066];
int cnt = 0;

bool cmp(Student a,Student b)
{
  if(a.tag!=b.tag)
    return a.tag>b.tag;
  else if(a.sum!=b.sum)
    return a.sum>b.sum;
  else if(a.perfect!=b.perfect)
    return a.perfect>b.perfect;
  else
    return a.ID<b.ID;
}

int main()
{
  memset(S,0,sizeof(S));
  memset(mp,-1,sizeof(mp));
  for(int i=0 ; i<10066 ; ++i)
    memset(S[i].score,-1,sizeof(S[i].score));    //将所有分数写为-1,最后判断输出“-”
  int n,k,m;   //n个人,k道题,提交量m
  scanf("%d%d%d",&n,&k,&m);
  for(int i=1 ; i<=k ; ++i)
    scanf("%d",&p[i]);   //存入分值
  for(int i=0 ; i<m ; ++i)
  {
    int name,score,testnum;
    scanf("%d %d %d",&name, &testnum, &score);
    
    if(mp[name]==-1)
    {
      mp[name] = cnt;
      cnt++;
    }
    S[mp[name]].ID=name;
    if(score!=-1)
      S[mp[name]].tag=1;
    if(S[mp[name]].score[testnum]==-1 && score==-1)          //编译没有成功过
      S[mp[name]].score[testnum]=0;
    if(S[mp[name]].score[testnum] < score)
    {
      if(score==p[testnum])                   //此题满分
        ++S[mp[name]].perfect;
      if(S[mp[name]].score[testnum]==-1)            //编译错误总分不算
        S[mp[name]].score[testnum]=0;
      S[mp[name]].sum+=score-S[mp[name]].score[testnum];   //更新总分
      S[mp[name]].score[testnum]=score;
    }
    
  }
  sort(S,S+cnt,cmp);

  S[0].rank=1;
  for(int i=1 ; i<cnt ; ++i)   //生成排名
  {
    if(S[i].sum==S[i-1].sum)   //【skill】分数不同,排名为i+1;分数相同,排名还是前一个,不变
      S[i].rank=S[i-1].rank;
    else
      S[i].rank=i+1;
  }

  for(int i=0 ; i<cnt && S[i].tag==1; ++i)   //输出
  {
    printf("%d %05d %d",S[i].rank, S[i].ID, S[i].sum);
    for(int j=1 ; j<=k ; ++j)
    {
      if(S[i].score[j]!=-1)   //不是-1输出成绩
        printf(" %d",S[i].score[j]);
      else   //是-1,输出“-”
        printf(" -");
    }
    printf("\n");
  }
  return 0;
}
posted on 2015-03-07 15:49  Evence  阅读(235)  评论(0编辑  收藏  举报