思路
1、对考生的准考证,因为是固定格式,使用sscanf函数来处理很方便。
2、对类型1:先按要求排序,再遍历输出即可,比较简单。
3、对类型2:同样是遍历输出即可,比较简单,都要注意没有结果时的输出即可,用flag来控制。
4、对类型3:这个相对难一点,建立一个type3结构体来进行统计处理,记录考场编号和人数,最后根据要求排序输出即可。
5、对类型3,需要进行日期的输出,注意要%06d,不然测试点1、4过不去。
AC代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct{//考生结构体
char str[14],level;
int examno,examdata,id,score;
}student;
typedef struct{//类型3统计使用
int examno,cnt;
}type3;
int cmp1(const void* a,const void* b){//按类型1的要求排序
student* pa=(student*)a;
student* pb=(student*)b;
if(pb->score == pa->score)return strcmp(pa->str,pb->str);
return pb->score - pa->score;
}
int cmp3(const void* a,const void* b){//按类型3的要求排序
type3* pa=(type3*)a;
type3* pb=(type3*)b;
if(pa->cnt == pb->cnt)return pa->examno - pb->examno;
return pb->cnt - pa->cnt;
}
int main(){
int n,m;
char temp[14];
scanf("%d %d%*c",&n,&m);
student st[n];
for(int i=0;i<n;i++){//接受学生数据并处理
scanf("%s %d%*c",st[i].str,&st[i].score);
sscanf(st[i].str,"%c%3d%6d%3d",&st[i].level,&st[i].examno,&st[i].examdata,&st[i].id);
}
qsort(st,n,sizeof(student),cmp1);
for(int i=1;i<=m;i++){
int flag=0,k=0,type,num;
char ch;
scanf("%d",&type);
if(type==1){//类型1
scanf("%*c%c",&ch);
printf("Case %d: %d %c\n",i,type,ch);
for(int j=0;j<n;j++){
if(st[j].level==ch){
printf("%s %d\n",st[j].str,st[j].score);
flag=1;
}
}
if(flag==0)printf("NA\n");//注意没有查询结果的输出
}
else if(type==2){//类型2
scanf("%d",&num);
printf("Case %d: %d %d\n",i,type,num);
int cnt=0,sum=0;
for(int j=0;j<n;j++){
if(st[j].examno==num){
flag=1;
cnt++;
sum+=st[j].score;
}
}
if(flag!=0)printf("%d %d\n",cnt,sum);
else printf("NA\n");//注意没有查询结果的输出
}
else{//类型3
scanf("%d",&num);
printf("Case %d: %d %06d\n",i,type,num);//注意输出日期时要%06d,不然测试点1和4过不了
type3 ty[1000];
for(int j=0;j<1000;j++)ty[j].cnt=0;//要初始化结构体的cnt,即每个考场指代的人数
for(int j=0;j<n;j++){//遍历学生,选择对应日期的进行统计
if(st[j].examdata==num){
ty[st[j].examno].examno=st[j].examno;//先用下标指代考场编号,之后再排序
ty[st[j].examno].cnt++;
}
}
qsort(ty,1000,sizeof(type3),cmp3);
for(int j=0;j<1000;j++){
if(ty[j].cnt!=0){//人数不为零的输出
printf("%d %d\n",ty[j].examno,ty[j].cnt);
flag=1;
}
else break;//遇到人数为零的就终止
}
if(flag==0)printf("NA\n");//注意没有查询结果的输出
}
}
return 0;
}