【1025】PAT Ranking (25 分)

简介: 【1025】PAT Ranking (25 分)【1025】PAT Ranking (25 分)
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>  
#include<map>
#include<vector>
#include<queue> 
using namespace std;  
注意该考场数组区间为[num-k,num)和其他边界
同分则后面的排名要隔后,局部和全局处理不同,后者按顺序边计算总排名,边输出所有考生的信息
struct Student{
  char id[15];  //准考证号
  int score;  //分数
  int location_number;  //考场号
  int local_rank;  //考场内排名
}stu[30010];
bool cmp(Student a,Student b){
  if(a.score != b.score ) return a.score>b.score; //先按分数从高到低排序
  else return strcmp(a.id,b.id)<0; //若分数相同,则按准考证号从小到大排序
}
int main(){   
  int n,k,num=0;  //num为总考生数!!!!
  scanf("%d",&n);  //n为考场数
  for(int i=1;i<=n;i++) {
    scanf("%d",&k);  //该考场内的人数
    for(int j=0;j<k;j++){
      scanf("%s %d",stu[num].id, &stu[num].score); 
      stu[num].location_number=i;  //该考生的考场号为i
      num++;  
    }
    sort(stu+num-k,stu+num,cmp);  //将该考场的考生排序
    stu[num-k].local_rank=1;  //该考场第1名的local_rank记为1
    for(int j=num-k+1;j<num;j++){  //对该考场剩余的考生
      if(stu[j].score == stu[j-1].score) { //如果与前一位考生同分
        stu[j].local_rank=stu[j-1].local_rank;  //loca_rank也相同
      }else{ //如果与前一位考生不同,local_rank为该考生前的人数
        stu[j].local_rank=j+1-(num-k) ;
      }
    }
  }
  printf("%d\n",num) ; //输出总考生数
  sort(stu,stu+num,cmp);  //将所有考生排序
  int r=1; //当前考生的排名
  for(int i=0;i<num;i++){  
    if(i>0 && stu[i].score != stu[i-1].score){
      r=i+1; //当前考生与上一个考生分数不同时,让r更新为人数+1
    }
    printf("%s ",stu[i].id);
    printf("%d %d %d\n",r,stu[i].location_number,stu[i].local_rank);
  }
  system("pause");
    return 0;   
}
相关文章
|
8月前
|
SQL 大数据 数据挖掘
深入解析力扣178题:分数排名(DENSE_RANK详解及模拟面试问答)
深入解析力扣178题:分数排名(DENSE_RANK详解及模拟面试问答)
【每日一题Day40】LC813最大平均值的和分组 | 序列DP
给定数组 nums 和一个整数 k 。我们将给定的数组 nums 分成 最多 k 个相邻的非空子数组 。 分数 由每个子数组内的平均值的总和构成。
110 0
LeetCode contest 200 5475. 统计好三元组 Count Good Triplets
LeetCode contest 200 5475. 统计好三元组 Count Good Triplets
|
测试技术
1004 成绩排名 (20 分)(sort)
1004 成绩排名 (20 分)(sort)
134 0
|
机器学习/深度学习 计算机视觉
LBP+GLCM+SVM对开源数据集kth_tips_col_200x200进行简单分类
LBP+GLCM+SVM对开源数据集kth_tips_col_200x200进行简单分类
569 0
LBP+GLCM+SVM对开源数据集kth_tips_col_200x200进行简单分类
【1141】PAT Ranking of Institutions (25分)【排序 map】
【1141】PAT Ranking of Institutions (25分)【排序 map】 【1141】PAT Ranking of Institutions (25分)【排序 map】
109 0
|
容器
【1012】The Best Rank (25 分)
【1012】The Best Rank (25 分) 【1012】The Best Rank (25 分)
121 0
【1063】Set Similarity (25 分)
【1063】Set Similarity (25 分) 【1063】Set Similarity (25 分)
105 0
【1118】Birds in Forest (25 分)
【1118】Birds in Forest (25 分) 【1118】Birds in Forest (25 分)
103 0