zoukankan      html  css  js  c++  java
  • 7-14 打印选课学生名单(25 分)

    假设全校有最多40000名学生和最多2500门课程。现给出每个学生的选课清单,要求输出每门课的选课学生名单。

    输入格式:

    输入的第一行是两个正整数:N(≤40000),为全校学生总数;K(≤2500),为总课程数。此后N行,每行包括一个学生姓名(3个大写英文字母+1位数字)、一个正整数C(≤20)代表该生所选的课程门数、随后是C个课程编号。简单起见,课程从1到K编号。

    输出格式:

    顺序输出课程1到K的选课学生名单。格式为:对每一门课,首先在一行中输出课程编号和选课学生总数(之间用空格分隔),之后在第二行按字典序输出学生名单,每个学生名字占一行。

    输入样例:

    10 5
    ZOE1 2 4 5
    ANN0 3 5 2 1
    BOB5 5 3 4 2 1 5
    JOE4 1 2
    JAY9 4 1 2 5 4
    FRA8 3 4 2 5
    DON2 2 4 5
    AMY7 1 5
    KAT3 3 5 4 2
    LOR6 4 2 4 1 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
    

    一开始用数组,全用数组,每次排序,最后一个测试点内存超限,时间也耗,后来先把名字排序,再记录,就好了。

    代码:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    struct stu
    {
        char name[5];
        int num;
        int choice[20];
    }s[40000];
    int cmp(const void *a,const void *b)
    {
        struct stu *aa = (void *)a,*bb = (void *)b;
        return strcmp(aa->name,bb->name)>0?1:-1;
    }
    typedef struct Node
    {
        int Data;
        struct Node *Next;
    }Node;
    int main()
    {
        int n,m;
        Node *head[2501][2];
        scanf("%d%d",&n,&m);
        for(int i = 1;i <= m;i ++)
        {
            head[i][0] = (Node *)malloc(sizeof(Node));
            head[i][0] -> Data = 0;
            head[i][0] -> Next = NULL;
            head[i][1] = head[i][0];
        }
        for(int i = 0;i < n;i ++)
        {
            scanf("%s",s[i].name);
            scanf("%d",&s[i].num);
            for(int j = 0;j < s[i].num;j ++)
            {
                scanf("%d",&s[i].choice[j]);
                head[s[i].choice[j]][0] -> Data += 1;
            }
        }
        qsort(s,n,sizeof(s[0]),cmp);
        for(int i = 0;i < n;i ++)
        {
            for(int j = 0;j < s[i].num;j ++)
            {
                Node *p = (Node *)malloc(sizeof(Node));
                p -> Data = i;
                p -> Next = NULL;
                head[s[i].choice[j]][1] -> Next = p;
                head[s[i].choice[j]][1] = p;
            }
        }
        for(int i = 1;i <= m;i ++)
        {
            printf("%d %d
    ",i,head[i][0] -> Data);
            head[i][1] = head[i][0] -> Next;
            while(head[i][1])
            {
                puts(s[head[i][1] -> Data].name);
                head[i][1] = head[i][1] -> Next;
            }
        }
    }
  • 相关阅读:
    java面向对象类的继承~ 匿名类 ;多态特性;强制类型转换
    Java面向对象~类和对象&方法,类方法
    1036. Escape a Large Maze
    909. Snakes and Ladders
    559. Maximum Depth of N-ary Tree
    987. Vertical Order Traversal of a Binary Tree
    913. Cat and Mouse
    865. Smallest Subtree with all the Deepest Nodes
    882. Reachable Nodes In Subdivided Graph
    864. Shortest Path to Get All Keys
  • 原文地址:https://www.cnblogs.com/8023spz/p/7705422.html
Copyright © 2011-2022 走看看