如有不对 不吝赐教
我可以毫不怀疑地说,这是我debug最烦,遇到问题最多的一个题目(当然,如果不用C的话,直接调用其他的语言库就没这么多麻烦的事情发生了)
下面进入正题:
假设全校有最多40000名学生和最多2500门课程。现给出每门课的选课学生名单,要求输出每个前来查询的学生的选课清单。
输入格式:
输入的第一行是两个正整数:N(≤40000),为前来查询课表的学生总数;K(≤2500),为总课程数。此后顺序给出课程1到K的选课学生名单。格式为:对每一门课,首先在一行中输出课程编号(简单起见,课程从1到K编号)和选课学生总数(之间用空格分隔),之后在第二行给出学生名单,相邻两个学生名字用1个空格分隔。学生姓名由3个大写英文字母+1位数字组成。选课信息之后,在一行内给出了N个前来查询课表的学生的名字,相邻两个学生名字用1个空格分隔。
输出格式:
对每位前来查询课表的学生,首先输出其名字,随后在同一行中输出一个正整数C,代表该生所选的课程门数,随后按递增顺序输出C个课程的编号。相邻数据用1个空格分隔,注意行末不能输出多余空格。
输入样例:
10 5
1 4
ANN0 BOB5 JAY9 LOR6
2 7
ANN0 BOB5 FRA8 JAY9 JOE4 KAT3 LOR6
3 1
BOB5
4 7
BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
5 9
AMY7 ANN0 BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
ZOE1 ANN0 BOB5 JOE4 JAY9 FRA8 DON2 AMY7 KAT3 LOR6
输出样例:
ZOE1 2 4 5
ANN0 3 1 2 5
BOB5 5 1 2 3 4 5
JOE4 1 2
JAY9 4 1 2 4 5
FRA8 3 2 4 5
DON2 2 4 5
AMY7 1 5
KAT3 3 2 4 5
LOR6 4 1 2 4 5
先上代码,再来慢慢分析
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define base 50 //课程数组的基数
#define delta 20 //课程数组的扩充量
struct Student{
char ch; //第三个字母
char num; //数字编码
short *course; //课程
short end; //最后一个课程的下标
short length; //课程数组的长度
struct Student *next; //下一个学生
};
struct Student *Insert(struct Student *student,char chIndentifier,char numIndentifier,short course);
int cmp(struct Student *s1,struct Student *s2);
void Free(struct Student *s);
void QuickSort(short *number,short left,short right);
int main(void)
{
int N,K; //学生总数 课程总数
int i,j;
int C; //选择每门课程的学生总数
short course;
char name[5]; //学生名字
struct Student *student[26][26]; //学生课表
struct Student *cur;
struct Student *temp=(struct Student *)malloc(sizeof(struct Student));
temp->course=NULL;
temp->end=-1;
temp->length=0;
temp->next=NULL; //由于比较
fscanf(stdin,"%d %d",&N,&K);
char allName[N][5]; //查询学生的名单
for(i=0