编程小菜鸡一枚,请教如何修改能通过测试点3、4。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char g1;//对cmp1排序所输入的全局字符
char g3[7];//对cmp3排序所输入的全局字符串
struct student{
char grade;//第 1 位是级别,即 T 代表顶级;A 代表甲级;B 代表乙级;
char room[4];//第 2~4 位是考场编号,范围从 101 到 999;
char date[7];//第 5~10 位是考试日期,格式为年、月、日顺次各占 2 位;
char id[4];//最后 11~13 位是考生编号,范围从 000 到 999
int marks;//分数
};
struct room{
char id[4];
int students;//考场人数
};
int cmp1(const void *a, const void *b){
struct student c = *(struct student *)a;
struct student d = *(struct student *)b;
if (c.grade!=d.grade)
{
if (c.grade==g1)
return -1;
else if (d.grade==g1)
return 1;
else
return c.grade-d.grade;
}
else//对于分数并列的考生,按其准考证号的字典序递增输出
{
if (c.marks!=d.marks)
return d.marks-c.marks;
if (strcmp(c.room,d.room)!=0)
return strcmp(c.room,d.room);
else
{
if (strcmp(c.date,d.date)!=0)
return strcmp(c.date,d.date);
else
return strcmp(c.id,d.id);
}
}
}
int cmp2(const void *a, const void *b){
struct room c = *(struct room *)a;
struct room d = *(struct room *)b;
if (c.students!=d.students)//输出按人数非递增顺序
return d.students-c.students;
else //若人数并列则按考场编号递增顺序输出
return strcmp(c.id,d.id);
}
int cmp3(const void *a, const void *b){
struct student c = *(struct student *)a;
struct student d = *(struct student *)b;
if (strcmp(c.date,d.date)!=0)
{
if (strcmp(c.date,g3)==0)
return -1;
else if (strcmp(d.date,g3)==0)
return 1;
else
return strcmp(c.date,d.date);
}
else
return strcmp(c.room,d.room);//考场编号递增顺序
}
int main(){
char temp[20];
int i,j,M,N,temp1;//考生人数M和统计要求的个数N
scanf("%d %d",&M,&N);
getchar();//接下来要读入字符串的一律用getchar
struct student S[M];
struct room R[M];
for (i=0;i<M;i++)//读入学生的信息
{
R[i].students = 0;//初始化
scanf("%s %d",temp,&S[i].marks);
getchar();
S[i].grade = temp[0];
for (j=0;j<3;j++)
S[i].room[j] = temp[1+j];
S[i].room[3] = '\0';
for (j=0;j<6;j++)
S[i].date[j] = temp[4+j];
S[i].date[6] = '\0';
for (j=0;j<3;j++)
S[i].id[j] = temp[10+j];
S[i].id[3] = '\0';
}
for (i=0;i<N;i++)
{
scanf("%d %s",&temp1,temp);
printf("Case %d: %d %s\n",i+1,temp1,temp);
switch (temp1){
case 1:
{
int isfind = 0;
g1 = temp[0];
qsort(S,M,sizeof(S[0]),cmp1);
for (j=0;j<M;j++)
{
if (S[j].grade!=g1)
break;
else
{
printf("%c%s%s%s %d\n",S[j].grade,S[j].room,S[j].date,S[j].id,S[j].marks);
isfind = 1;
}
}
if (isfind==0)
printf("NA\n");
break;
}
case 2:
{
int num = 0;
int sum = 0;
for (j=0;j<M;j++)
{
if (strcmp(S[j].room,temp)==0)
{
num++;
sum+=S[j].marks;
}
}
if (num==0)
printf("NA\n");
else
printf("%d %d\n",num,sum);
break;
}
case 3:
{
int k = -1;//统计符合日期的不同的考场个数
strcpy(g3,temp);
qsort(S,M,sizeof(S[0]),cmp3);
for (j=0;j<M;j++)
{
if (strcmp(S[j].date,temp)!=0)//日期不相等说明不符号要求
break;
if (j==0)
{
k++;
strcpy(R[k].id,S[j].room);
R[k].students++;
}
else
{
if (strcmp(S[j].room,R[k].id)==0)
R[k].students++;
else
{
k++;
strcpy(R[k].id,S[j].room);
R[k].students++;
}
}
}
if (k==-1)
printf("NA\n");
else
{
k++;//下标转换为数组的长度
qsort(R,k,sizeof(R[0]),cmp2);
for (j=0;j<k;j++)
printf("%s %d\n",R[j].id,R[j].students);
break;
}
break;
}
default:break;
}
}
return 0;
}