1047 Student List for Course (25 分)

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

 我自己的思路(用容器做) 如示:(最后一个测试点超时,这个给的时间限制还是1000ms)

判断为使用了map,使用容器才超时的。具体原因现在还不会,大致了解:

#include <iostream>
#include <map>
#include <set>	
#include <string>
#include <algorithm>	
#include <time.h>
#include <cstring>

using namespace std;

char name[40010][5];

//bool cmp(char a[],char b[]) {
//	return strcmp(a,b)<0;
//}

int main()
{
	//freopen("in.txt", "r", stdin);
	int sNum, cNum;
	string finalName;

	
	scanf("%d %d", &sNum, &cNum);
	//cin >> sNum >> cNum;
	map<int, set<string>> m;
	
	for (int i = 0; i < sNum; i++)
	{
		int cnt, course;
		
		//cin >> name;
		/*allS[i] = name;
		int tempName = i;*/
		scanf("%s ", name[i]);
		finalName = name[i];
		scanf("%d", &cnt);
		//cin >> cnt;
		for (int j = 0; j < cnt; j++)
		{
			scanf("%d", &course);
			//cin >> course;
			m[course].insert(finalName);
			
		}		
	}
	
	
	
	for (int i = 1; i <= cNum; i++)
	{
		//cout << i << " " << m[i].size() << endl;
		printf("%d %d\n", i, m[i].size());
		//sort(m[i].begin(), m[i].end(),cmp);
		set<string>::iterator it = m[i].begin();
		for (;it!=m[i].end();it++)
		{
			//cout << (*it) << endl;
			/*char buf[5];
			length*/
			printf("%s\n", (*it).c_str());//string.c_str()  在输入的时候用 无错输出 以字符数组的形式,但是在string转换为字符数组的时候,不能这样直接用。
		}
	}
	
	
	//fclose(stdin);
	return 0;
}

 以为是用了cin才超时,所以讲cin都改为scanf结果还是最后测试点超时。

最开始用time,测过代码的运行时间,各种走弯路:

还将name变成char *类型,map中的set的类型也相应变为char *,最后在输出的时候,名字输出不能按照字典序,还写了cmp比较函数,想通过sort对set中的char *进行排序,最后发现好像不能这样。在上面的代码中有体现。

#include <iostream>
#include <map>
#include <set>	
#include <string>
#include <algorithm>	
#include <time.h>

using namespace std;

int main()
{
	//freopen("in.txt", "r", stdin);
	int sNum, cNum;
	//string name;
	char name[40010][5];
	//string allS[40060];
	//scanf("%d %d", &sNum, &cNum);
	cin >> sNum >> cNum;
	map<int, set<char *>> m;
	//clock_t start, end;
	//start = clock();
	//cout << "start"<<start<<endl;
	for (int i = 0; i < sNum; i++)
	{
		int cnt, course;
		
		//cin >> name;
		/*allS[i] = name;
		int tempName = i;*/
		scanf("%s ", name[i]);
		
		scanf("%d", &cnt);
		//cin >> cnt;
		for (int j = 0; j < cnt; j++)
		{
			//scanf("%d", &course);
			cin >> course;
			m[course].insert(name[i]);
			
		}		
	}
	//time = clock();
	
	//sort(m.begin(), m.end());
	for (int i = 1; i <= cNum; i++)
	{
		cout << i << " " << m[i].size() << endl;
		//printf("%d %d", i, m[i].size());
		set<char *>::iterator it = m[i].begin();
		for (;it!=m[i].end();it++)
		{
			cout << (*it) << endl;
			//printf("%s\n", (*it));
		}
	}
	//end = clock();
	//double time = (double)((end - start)*1000 / CLOCKS_PER_SEC);//测代码运行时间
	//cout << "time:" << time << endl;
	//fclose(stdin);
	return 0;
}

Note: string类型和字符数组的相互转换:(c语言里没有string类型,只有字符数组-char a[],用scanf输入的时候不能直接赋值给string类型,printf输出的时候也不能直接输出string类型)

char *即字符数组转换为string ,可以直接赋值给string类型就好了。

如果要将string转换为char*,可以使用string提供的函数c_str() ,或是函数data(),data除了返回字符串内容外,不附加结束符'\0',而c_str()返回一个以‘\0’结尾的字符数组。

c_str()函数返回一个指向正规C字符串的指针,内容与本string串相同.

这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式.

注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针

比如:最好不要这样:

char* c;string s="1234";c = s.c_str(); //c最后指向的内容是垃圾,因为s对象被析构,其内容被处理

应该这样用:

char c[20];string s="1234";strcpy(c,s.c_str());这样才不会出错,c_str()返回的是一个临时指针,不能对其进行操作

再举个例子:

c_str() 以 char* 形式传回 string 内含字符串如果一个函数要求char*参数,可以使用c_str()方法:string s = "Hello World!";printf("%s",s.c_str()); //输出 "Hello World!"

#include <iostream>
#include <vector>
#include <algorithm>	

using namespace std;

char name[40060][5];

bool cmp(int a, int b)
{
	return strcmp(name[a], name[b]) < 0;  //这个地方要注意  要理解透彻
	//return name[a] < name[b];
}

int main()
{
	//freopen("in.txt", "r", stdin);
	int sNum, cNum;

	vector<int> course[2001];
	cin >> sNum >> cNum;
	for (int i = 0; i < sNum; i++)
	{
		scanf("%s", name[i]);
		int cnt;
		scanf("%d", &cnt);
		for (int j = 0; j < cnt; j++)
		{
			int courseNo;
			scanf("%d", &courseNo);
			course[courseNo].push_back(i);
		}
	}
	
	for (int i = 1; i <=cNum; 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]]); //写的时候这个地方写成了name[j],不够仔细。
		}
	}
	//fclose(stdin);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是Python代码实现: ```python class Student: def __init__(self, name, id): self.name = name self.id = id self.courses = [] def enroll(self, course): self.courses.append(course) def drop(self, course): self.courses.remove(course) def list_courses(self): print(f"{self.name}'s courses:") for course in self.courses: print(course.name) class Course: def __init__(self, name, teacher): self.name = name self.teacher = teacher self.students = [] def add_student(self, student): self.students.append(student) student.enroll(self) def remove_student(self, student): self.students.remove(student) student.drop(self) def list_students(self): print(f"Students enrolled in {self.name}:") for student in self.students: print(student.name) class Teacher: def __init__(self, name, id): self.name = name self.id = id self.courses = [] def add_course(self, course): self.courses.append(course) def remove_course(self, course): self.courses.remove(course) def list_courses(self): print(f"{self.name}'s courses:") for course in self.courses: print(course.name) ``` 使用示例: ```python # 创建学生、课程和教师实例 alice = Student("Alice", 1) bob = Student("Bob", 2) math = Course("Math", Teacher("Mr. Smith", 101)) english = Course("English", Teacher("Mrs. Johnson", 102)) mr_jones = Teacher("Mr. Jones", 103) # 学生选课 math.add_student(alice) math.add_student(bob) english.add_student(alice) # 教师管理课程 mr_jones.add_course(math) mr_jones.add_course(english) # 列出学生和课程信息 alice.list_courses() # Alice's courses: Math, English bob.list_courses() # Bob's courses: Math math.list_students() # Students enrolled in Math: Alice, Bob english.list_students() # Students enrolled in English: Alice mr_jones.list_courses() # Mr. Jones's courses: Math, English # 学生退课 math.remove_student(bob) bob.list_courses() # Bob's courses: math.list_students() # Students enrolled in Math: Alice ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值