【PAT】A1047 Student List for Course

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

 解题思路:

  1. 理解题意&样例分析:本题与A1039恰好是反着来的,给你每一位同学的选课数目和课程编号,需要你给出每一门课程选的学生,且学生的排序按字母表升序。代码中可以找一下和A1039的不同和为什么要这么做(比如,在A1039中,学生的姓名可以转化为整型数字表示,而在本题中则直接中二维数组表示,这是为什么?原因是目的不同,条件不同。在1030中,最后要查询的学生的姓名是给你的,可以根据学生姓名转化为整型数(散列思想,唯一标识),这样可以更好的查找和输出;而在本题中,给你的是姓名和课程编号就不能这样做了,直接用二维数组表示学生姓名会更好一些)
  2. 代码分为3段:首先是输入,根据给出的课程号为横向的编号,用vector记录在数组中,每一行都记录着选着一门课的学生的编号,学生编号和姓名都存在另一个二维数组中;然后整理数据,排序;最后按要求输出数据。这里就不难了
  3. 总结:本题的难点就在于设置的两个数组,从本质上看就是两个两位数组。第一个数组也就是记录学生序号和姓名的数组,序号指的是每一位学生姓名的存储行的位置;第二个数组是课程的可变长的二维数组vector(节省空间),当输入一门课程编号后,把学生的序号push_back()到该行(行号为课程编号)。最后行内排序也是,sort(course[i].begin(),course[i].end,cmp).

代码:

#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
vector<int> course[2510];//2510行的可变长数组(二维) ,存放第i门课的情况 
char name[40010][6];

bool cmp(int a,int b){
	return strcmp(name[a],name[b])<0;//*字符串的比较 
}

int main(){
	int m,n,num,courseID;
	scanf("%d%d",&m,&n);//m个学生,n种课
	for(int i=0;i<m;i++){
		scanf("%s%d",name[i],&num);//获得学生的姓名和选的课数 
		for(int j=0;j<num;j++){
			scanf("%d",&courseID); //获得课的编号 
			course[courseID].push_back(i);  //*i是学生姓名的编号 
		}
	}
	for(int i=1;i<=n;i++){  //* 
			printf("%d %d\n",i,course[i].size());
			sort(course[i].begin(),course[i].end(),cmp);
			for(int j=0;j<course[i].size();j++){
				printf("%s\n",name[course[i][j]]);
			}
	} 
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_之桐_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值