1039 Course List for Student

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805447855292416

开散列的方法是我从别人博客学到的,也是第一次用,感觉挺好用的,下次遇见这样的题目就有思路了。

然后里面sync_with_stdio(false)和下面的那句话是为了加速输入输出的,这是c++兼容c输入输出的方式,但是在测试的时候,oj会报段错误,我测试了一下,只有这两句话还是会报错,之前我也用过这样的语句,没报错,这里是为什么呢?

代码如下:

 1 #include<iostream>
 2 #include<vector>
 3 #include<stdio.h>
 4 #include<fstream>
 5 #include<algorithm>
 6 #include<cstdlib>
 7 using namespace std;
 8 const int maxn = 26*26*26+10;
 9 int trans(char name[]){
10     int ret = 0;
11     for(int i = 0; i < 3; i++){
12         ret = ret*26+name[0]-'A';
13     }
14     ret += name[3]-'0';
15     return ret;
16 }
17 vector<int> v[maxn];
18 int main(){
19     //ios::sync_with_stdio(false);
20     //cin.tie(0);
21     int n,m,k,l;
22     int stem;
23     char name[5];
24     cin >> n >> m;
25     for(int i = 0; i < m; i++){
26         cin >> k >> l;
27         for(int j = 0; j < l; j++){
28             scanf("%s",name);
29 //            cin >> name;
30             stem = trans(name);
31             v[stem].push_back(k);
32 //            cout << stem << endl;
33         }
34     }
35     vector<int>::iterator it;
36     for(int i = 0; i < n; i++){
37         scanf("%s",name);
38 //        cin >> name;
39         stem = trans(name);
40         cout << name << " " << v[stem].size();
41         sort(v[stem].begin(),v[stem].end());
42         for(it = v[stem].begin(); it != v[stem].end(); it++){
43             cout << " " << *it;
44         }
45         cout << endl;
46     }
47     return 0;
48 }

 

转载于:https://www.cnblogs.com/huhusw/p/9755356.html

以下是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 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值