1047 Student List for Course (25 point(s)) - C语言 PAT 甲级

1047 Student List for Course (25 point(s))

Zhejiang University has 40,000 students and provides 2,500 courses. Now given the registered course list of each student, you are supposed to output the student name lists of all the courses.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 numbers: N (≤ 40,000), the total number of students, and K (≤ 2,500), the total number of courses. Then N lines follow, each contains a student’s name (3 capital English letters plus a one-digit number), a positive number C (≤ 20) which is the number of courses that this student has registered, and then followed by C course numbers. For the sake of simplicity, the courses are numbered from 1 to K.

Output Specification:

For each test case, print the student name lists of all the courses in increasing order of the course numbers. For each course, first print in one line the course number and the number of registered students, separated by a space. Then output the students’ names in alphabetical order. Each name occupies a line.

Sample Input:

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

Sample Output:

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

题目大意:

输入 N 个学生,K 门课程,每名学生选择 C 门课程,输出每门课程下有多少学生,学生姓名按字典序排序

设计思路:

1039 题类似。

思路 3(链表):

  • 学生信息用链表储存
  • 输出需要排序时,将链表转化为数组
  • 节点中未直接存字符串,排序过程中,不需要比较、交换字符串,(但似乎影响不大)

思路 2(取巧的方法):

  • 通过多次提交,可以确定所有课程中人数最多的为 398 人
  • 直接用一个三维数组存储信息,排序后输出即可

思路 1(位运算超时):

  • 使用位运算储存每门课的学生信息
  • 每门课下学生人数很少,即使用位记录学生信息同样造成很大的空间和时间的浪费
编译器:C (gcc)
// 思路 3(链表):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int cmp(const void *a, const void *b);
int toid(char s[]);

#define IDMAX (26 * 26 * 26 * 10)

struct course {
        int sum;
        struct stu *next;
};

struct stu {
        int name;
        struct stu *next;
};

struct course courses[2500] = {0};
char names[IDMAX][5] = {0};
int ans[40000] = {0};

int main(void)
{
        int n, k;
        char name[5] = {0};
        int c, id, num;
        struct stu *student = NULL;
        int i, j, v;

        scanf("%d %d", &n, &k);
        for (i = 0; i < n; i++) {
                scanf("%s %d", name, &c);
                id = toid(name);
                strcpy(names[id], name);
                for (j = 0; j < c; j++) {
                        scanf("%d", &num);
                        num--;
                        student = (struct stu*)malloc(sizeof(struct stu));
                        student->name = id;
                        student->next = courses[num].next;
                        courses[num].next = student;
                        courses[num].sum++;
                }
        }

        for (i = 0; i < k; i++) {
                printf("%d %d\n", i + 1, courses[i].sum);
                student = courses[i].next;
                j = 0;
                while (student) {
                        ans[j] = student->name;
                        student = student->next;
                        j++;
                }
                qsort(ans, j, sizeof(ans[0]), cmp);
                for (v = 0; v < j; v++) {
                        printf("%s\n", names[ans[v]]);
                }
        }
        return 0;
}

int cmp(const void *a, const void *b)
{
        return *(int *)a - *(int *)b;
}

int toid(char s[])
{
        int id = 0;
        int i;
        for (i = 0; i < 3; i++) {
                id = id * 26 + (s[i] - 'A');
        }
        id = id * 10 + (s[i] - '0');
        return id;
}

/*//思路 2(取巧的方法):
#include <stdio.h>
#include <string.h>

int cmp(const void *a, const void *b)
{
        return strcmp(a, b);
}

char names[2500][398][5] = {0};
int sum[2500] = {0};

int main(void)
{
        int n, k;
        char s[5] = {0};
        int c, num;
        int i, j;

        scanf("%d %d", &n, &k);
        for (i = 0; i < n; i++) {
                scanf("%s %d", s, &c);
                for (j = 0; j < c; j++) {
                        scanf("%d", &num);
                        num--;
                        strcpy(names[num][sum[num]], s);
                        sum[num]++;
                }
        }

        for (i = 0; i < k; i++) {
                printf("%d %d\n", i + 1, sum[i]);
                qsort(names[i], sum[i], sizeof(names[0][0]), cmp);
                for (j = 0; j < sum[i]; j++) {
                        puts(names[i][j]);
                }
        }

        return 0;
}
*/

/*//思路 1(位运算超时):
#include <stdio.h>
#include <string.h>

#define IDMAX (26 * 26 * 26 * 10)

struct course {
        char c[IDMAX >> 3];
        int sum;
};

struct course courses[2500] = {0};
char names[IDMAX][5] = {0};

int main(void)
{
        int n, k;
        char name[5] = {0};
        int c, id, num;
        int i, j, v;

        scanf("%d %d", &n, &k);
        for (i = 0; i < n; i++) {
                scanf("%s %d", name, &c);
                id = toid(name);
                strcpy(names[id], name);
                for (j = 0; j < c; j++) {
                        scanf("%d", &num);
                        num--;
                        courses[num].c[id >> 3] |= (128 >> (id & (8 - 1)));
                        courses[num].sum++;
                }
        }

        for (i = 0; i < k; i++) {
                printf("%d %d\n", i + 1, courses[i].sum);
                for (j = 0; j < (IDMAX >> 3); j++) {
                        for (v = 0; v < 8; v++) {
                                if ((courses[i].c[j] & (128 >> v)) != 0) {
                                        printf("%s\n", names[j * 8 + v]);
                                }
                        }
                }
        }
        return 0;
}

int toid(char s[])
{
        int id = 0;
        int i;
        for (i = 0; i < 3; i++) {
                id = id * 26 + (s[i] - 'A');
        }
        id = id * 10 + (s[i] - '0');
        return id;
}
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值