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

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

Zhejiang University has 40000 students and provides 2500 courses. Now given the student name lists of all the courses, you are supposed to output the registered course list for each student who comes for a query.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers: N (≤ 40,000), the number of students who look for their course lists, and K (≤ 2,500), the total number of courses. Then the student name lists are given for the courses (numbered from 1 to K) in the following format: for each course i, first the course index i and the number of registered students Ni (≤ 200) are given in a line. Then in the next line, Ni student names are given. A student name consists of 3 capital English letters plus a one-digit number. Finally the last line contains the N names of students who come for a query. All the names and numbers in a line are separated by a space.

Output Specification:

For each test case, print your results in N lines. Each line corresponds to one student, in the following format: first print the student’s name, then the total number of registered courses of that student, and finally the indices of the courses in increasing order. The query results must be printed in the same order as input. All the data in a line must be separated by a space, with no extra space at the end of the line.

Sample Input:

11 5
4 7
BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
1 4
ANN0 BOB5 JAY9 LOR6
2 7
ANN0 BOB5 FRA8 JAY9 JOE4 KAT3 LOR6
3 1
BOB5
5 9
AMY7 ANN0 BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
ZOE1 ANN0 BOB5 JOE4 JAY9 FRA8 DON2 AMY7 KAT3 LOR6 NON9

Sample Output:

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
NON9 0

题目大意:

有 N 位同学要查询自己的选课情况,有 K (<= 2500) 门课程,给出课程编号 i 和选择此门课程的学生人数 Ni (<= 200) 及姓名;学生姓名由 3 个大写字母和 1 个数字组成;

对最后一行的查询,按查询顺序给出对应学生的选课情况;

设计思路:
  1. 名字按照 26*26*26*10 映射为数字,用 id 数组记录每个人的序号,序号按照每个人的出现次序依次递增;
  2. 用结构体数组按照学生序号记录每位学生的选课情况;
    1. 有 2500 门课,直接用 char c[2500] 记录绝对超内存,所以用 1 bit 记录一门课,需要用位运算;
    2. 进行位运算时,可以把 char c[313] 看作 bit c[313][8],两个维度的下标均从 0 开始,所以对于课程编号进行减 1 处理 course--;
  3. 按查询顺序输出;
  4. c[course >> 3] |= (128 >> (course & (8 - 1)):
    1. course >> 3,课程编号除以 8,得到 c[][] 一维下标
    2. course & ( 8 - 1),课程编号对 8 取余后移位,相当于得到 c[][] 二维下标;
    3. c[course >> 3] |= (128 >> (course & (8 - 1)),移位运算,或运算,相当于把 c[][] 对应位赋值为 1;

注意:

  1. 题目中 40000 仅是需要查询的学生数目最大值,学生数目最大值依旧是 26*26*26*10,最开始结构数组只给了 40000,导致最后一个测试点段错误;
  2. 进行位运算时,可以把 char c[313] 看作 bit c[313][8],两个维度的下标均从 0 开始,所以对于课程编号进行减 1 处理 course--,对位运算不熟悉,开始没有减 1 处理,结果都是错的;
  3. 是否还有其他思路记录课程信息?望大神告知!
编译器:C (gcc)
#include <stdio.h>

struct student {
        char c[313];
        int sum;
};

struct student stus[26 * 26 * 26 * 10 + 10] = {0};
int id[26 * 26 * 26 * 10 + 10] = {0}, count = 0;
int main(void)
{
        int n, k;
        int course, m;
        int i, j, v;
        char name[5];

        scanf("%d %d", &n, &k);
        for (i = 0; i < k; i++) {
                scanf("%d %d", &course, &m);
                course--;
                for (j = 0; j < m; j++) {
                        scanf(" %s", name);
                        int temp = toid(name);
                        stus[temp].c[course >> 3] |= (128 >> (course & (8 - 1)));
                        stus[temp].sum++;
                }
        }

        for (i = 0; i < n; i++) {
                scanf(" %s", name);
                int temp = toid(name);
                printf("%s %d", name, stus[temp].sum);
                for (j = 0; j < 313; j++) {
                        for (v = 0; v < 8; v++) {
                                if ((stus[temp].c[j] & (128 >> v)) != 0) {
                                        printf(" %d", j * 8 + v + 1);
                                }
                        }
                }
                printf("\n");
        }

        return 0;
}

int toid(char s[])
{
        int n = 0;
        int i = 0, temp = 0;

        for (i = 0; i < 3; i++) {
                n = n * 26 + (s[i] - 'A');
        }
        n = n * 10 + (s[i] - '0');
        if (id[n] == 0) {
                count++;
                id[n] = count;
        }

        return id[n];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值