数据结构实验之排序七:选课名单
#include #include #include struct node { char name[11]; struct node*next; }; int main() { int n,m,a,x;//n为学生总人数,m为课程总数 char str[12]; scanf("%d%d",&n,&m); struct node*p,*q; struct node*head[2001];//存储每门课学生姓名 int i; for(i=1;i<=2000;i++) { head[i]=(struct node*)malloc(sizeof(struct node)); head[i]->next=NULL;//初始化 目前没有学生 } int num[2001];//存储选修每门课的人数 memset(num,sizeof(num)); while(n--) { scanf("%s %d",str,&a);//s为姓名,a为此学生选课总数 while(a--)//a个课程编号 { scanf("%d",&x); num[x]++;//次门课选修人数纪录 p=head[x]; q=(struct node*)malloc(sizeof(struct node)); strcpy(q->name,str); while(p->next) { if(strcmp(q->name,p->next->name)<0) break; p=p->next; } q->next=p->next; p->next=q; } } for(i=1;i<=m;i++) { printf("%d %dn",i,num[i]); p=head[i]->next; while(p) { printf("%sn",p->name); p=p->next; } } return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |