1039 Course List for Student (25分) && 1047 Student List for Course (25分)

3 篇文章 0 订阅

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 N​i​​(≤200) are given in a line. Then in the next line, N​i
​​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

笔记写代码里面了

#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;

const int N = 40010;
const int M = 26 * 26 * 26 * 10 + 1;  //由姓名散列成的数字的上界 
vector<int> selectCourse[M];  //记录每个学生选择的课程编号 
//vector<int> selectCourse[M]相当于定义了一个动态的二维数组,a[m][n], 相当于n不定长,m定长 
//即 selectCourse[]中的每一个元素都是一个vector

int getID(char name[]) { //hash函数,即散列函数,将姓名散列成数字,可以用作数组元素的位置 
	int id = 0;
	for(int i = 0; i < 3; ++i) {
		id = id * 26 + (name[i] - 'A');
	}
	id = id * 10 + (name[3] - '0');
	return id;
} 

int main() {
	char name[5];
	int n, k;
	scanf("%d %d", &n, &k); //输入人数以及课程数
	for(int i = 0; i < k; ++i) {
		int course, x;
		scanf("%d %d", &course, &x);  //输入课程编号以及选课人数
		for(int j = 0; j < x; ++j) {
			scanf("%s", name);  //输入学生姓名
			int id = getID(name);  //将姓名散列成一个编号
			selectCourse[id].push_back(course); //将该课程编号加入学生的选择中 
		} 
	} 
	
	for(int i = 0; i < n; ++i) {
		scanf("%s", name);
		int id = getID(name);
		sort(selectCourse[id].begin(), selectCourse[id].end());  //将该学生选择的课程编号从小到大排序
		printf("%s %d", name, selectCourse[id].size()); //输出姓名和选棵数 
		for(int j = 0; j < selectCourse[id].size(); ++j) { //依次输出课程编号 
			printf(" %d", selectCourse[id][j]);
		} 
		printf("\n");
        
	} 
	
	return 0;
} ```
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;

const int N = 40010;
const int M = 26 * 26 * 26 * 10 + 1;  //由姓名散列成的数字的上界 
vector<int> selectCourse[M];  //记录每个学生选择的课程编号 
//vector<int> selectCourse[M]相当于定义了一个动态的二维数组,a[m][n], 相当于n不定长,m定长 
//即 selectCourse[]中的每一个元素都是一个vector

int getID(char name[]) { //hash函数,即散列函数,将姓名散列成数字,可以用作数组元素的位置 
	int id = 0;
	for(int i = 0; i < 3; ++i) {
		id = id * 26 + (name[i] - 'A');
	}
	id = id * 10 + (name[3] - '0');
	return id;
} 

int main() {
	char name[5];
	int n, k;
	scanf("%d %d", &n, &k); //输入人数以及课程数
	for(int i = 0; i < k; ++i) {
		int course, x;
		scanf("%d %d", &course, &x);  //输入课程编号以及选课人数
		for(int j = 0; j < x; ++j) {
			scanf("%s", name);  //输入学生姓名
			int id = getID(name);  //将姓名散列成一个编号
			selectCourse[id].push_back(course); //将该课程编号加入学生的选择中 
		} 
	} 
	
	for(int i = 0; i < n; ++i) {
		scanf("%s", name);
		int id = getID(name);
		sort(selectCourse[id].begin(), selectCourse[id].end());  //将该学生选择的课程编号从小到大排序
		printf("%s %d", name, selectCourse[id].size()); //输出姓名和选棵数 
		for(int j = 0; j < selectCourse[id].size(); ++j) { //依次输出课程编号 
			printf(" %d", selectCourse[id][j]);
		} 
		printf("\n");
        
	} 
	
	return 0;
} 

#include<iostream>
#include<string>
#include<map>
#include<vector> 
#include<set>
#include<algorithm>
using namespace std;

//想用map试一下A1039来着,但是搞不懂map之后能否使用vector的push_back 
//可以,只是直接打了"."之后调用出来的是map的函数,要自己敲上push_back
//用map最后一组数据超时 
/*using namespace std;

const int N = 40010;
map<string, vector<int> > selectCourse;
map<string, set<int> > mp;
int main() {
	string name;
	int n, k;
	scanf("%d %d", &n, &k);
	for(int i = 0; i < k; ++i) {
		int course, x;
		scanf("%d %d", &course, &x);
		for(int j = 0; j < x; ++j) {
			cin >> name;
			selectCourse[name].push_back(course);
		}
	}
	
	for(int i = 0; i < n; ++i) {
		cin >> name;
		cout << name << " "<<selectCourse[name].size();
		sort(selectCourse[name].begin(), selectCourse[name].end());
		for(int j = 0; j < selectCourse[name].size(); ++j) { //依次输出课程编号 
			cout << " " <<selectCourse[name][j];
		} 
		printf("\n");
	}
	
}*/
下面是1047题,两道题互为相反,前者统计学生选课数目,后者统计该课程学生数目,放在一起对比学习
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

```cpp
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;

const int N = 40005;
const int M = 2505;

vector<int> course[M]; //course[]存放第i门课的所有学生的编号
//阿么要如何解决学生的编号与姓名之间的转换呢?
//编号接收学生姓名即可
char name[N][5];

bool cmp(int a, int b) {
	return strcmp(name[a], name[b]) < 0; //将姓名按照字典顺序从小到大排序 
} 

int main()  {
	int n, k;
	scanf("%d %d", &n, &k); //输入学生人数以及课程数目
	int courseNum, courseID;
	for(int i = 0; i < n; ++i) {
		scanf("%s %d", name[i], &courseNum); //输入学生姓名以及学生多选课程数目
		for(int j = 0; j < courseNum; ++j) {
			scanf("%d", &courseID); //输入所选课程编号 
			course[courseID].push_back(i);  //将第i个学生加入第courseID课程 
		} 
	} 
	
	for(int i = 1; i <= k; ++i) {
		printf("%d %d\n", i, course[i].size());
		sort(course[i].begin(), course[i].end(), cmp); 
		//将学生的编号通过cmp函数转化为姓名进行按字典顺序排序,最后返回时使得对应编号排序 
		for(int j = 0; j < course[i].size(); ++j) {
			printf("%s\n", name[course[i][j]]); //输出学生姓名 
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值