数据结构实验之排序七:选课名单

来源:互联网 发布:ug五轴编程工资多少钱 编辑:程序博客网 时间:2024/05/29 18:17

题目描述

随着学校规模的扩大,学生人数急剧增加,选课名单的输出也成为一个繁重的任务,我校目前有在校生3万多名,两千多门课程,请根据给定的学生选课清单输出每门课的选课学生名单。

输入

输入第一行给出两个正整数N( N ≤ 35000)和M(M ≤ 2000),其中N是全校学生总数,M是课程总数,随后给出N行,每行包括学生姓名拼音+学号后两位(字符串总长度小于10)、数字S代表该学生选课的总数,随后是S个课程编号,约定课程编号从1到M,数据之间以空格分隔。

 

输出

按课程编号递增的顺序输出课程编号、选课总人数以及选课学生名单,对选修同一门课程的学生按姓名的字典序输出学生名单。数据之间以空格分隔,行末不得有多余空格。

示例输入

5 3Jack01 2 2 3Jone01 2 1 3Anni02 1 1Harry01 2 1 3TBH27 1 1

示例输出

1 4Anni02Harry01Jone01TBH272 1Jack013 3Harry01Jack01Jone01

提示



#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef struct node
{
    char name[11];
    node *next;
}node;
int num[2001];//统计选课的总人数;
node *nam[2001];//统计选课的学生名单;
int main()
{
    char str[11];
    int n,m,s,shu;
    int i;
    while(~scanf("%d%d",&n,&m))
    {
        memset(num,0,sizeof(num));//人数的初始化;
        for(i=0;i<2001;i++)
        {
            nam[i]=new node;//名字链表的的初始化;
            nam[i]->next=NULL;
        }
        for(i=0;i<n;i++)
        {
            scanf("%s%d",str,&s);
            while(s--)
            {
                scanf("%d",&shu);
                num[shu]++;//选这门课的人数加1;
                node *q=new node;//记录选这门课的人名;
                q->next=NULL;
                strcpy(q->name,str);
                node *p=nam[shu];//学生名单;
                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 %d\n",i,num[i]);
            node *p=nam[i]->next;//人名链;
            while(p)
            {
                printf("%s\n",p->name);
                p=p->next;
            }
        }
    }
    return 0;
}


  1. #include<bits/stdc++.h>  
  2. using namespace std;  
  3. vector<string>vt[2010];  
  4. int M[2010];  
  5. bool cmp(string x ,string y)  
  6. {  
  7.     return x < y ;  
  8. }  
  9. int main()  
  10. {  
  11.   int i,j,n,m,k,t;  
  12.   string temp;  
  13.   scanf("%d %d",&n,&m);  
  14.   memset(M,0,sizeof(M));  
  15.   for(i=0;i<n;i++)  
  16.   {  
  17.       cin>>temp;  
  18.       cin>>k;  
  19.       for(j=0;j<k;j++)  
  20.       {  
  21.           cin>>t;  
  22.           vt[t].push_back(temp);  
  23.           M[t]++;  
  24.       }  
  25.   
  26.   }  
  27.   for(i=0;i<2010;i++)  
  28.   {  
  29.       if(M[i]!=0)  
  30.        {  
  31.            sort(vt[i].begin(),vt[i].end(),cmp);  
  32.            cout<<i<<" "<<M[i]<<endl;  
  33.            for(j=0;j<vt[i].size();j++)  
  34.             cout<<vt[i][j]<<endl;  
  35.        }  
  36.   }  
  37. }  
01#include<cstring>
02#include<iostream>
03#include<cstdio>
04#include<vector>
05#include<algorithm>
06#include<set>
07using namespace std;
08set<string>v[2010];
09int num[2010];
10 
11int cmp(string a,string b)
12{
13 return a<b;
14}
15 
16int main()
17{
18     int i,j,n,m,k,t;
19     string s;
20     cin>>n>>m;
21     for(i=0;i<n;i++)
22     {
23       cin>>s;
24       cin>>k;
25       for(j=0;j<k;j++)
26       {
27        cin>>t;
28        v[t].insert(s);
29        num[t]++;
30       }
31     }
32     for(i=0;i<2010;i++)
33     {
34       if(num[i])
35       {
36    printf("%d %d\n",i,num[i]);
37    set<string>::iterator rit;
38     for(rit=v[i].begin();rit!=v[i].end();rit++)
39       cout<<*rit<<endl;
40       }
41     }
42}
43 
44 
45/***************************************************
46User name: *********
47Result: Wrong Answer
48Take time: 296ms
49Take Memory: 14316KB
50Submit time: 

0 0
原创粉丝点击