0
点赞
收藏
分享

微信扫一扫

A1039 Course List for Student (25 分)

生态人 2023-01-09 阅读 126


思路:

  1. 学生的姓名利用字符串hash求解,由于题中所给的是 三位大写数字+一位字母 ,所以可利用散列知识,开辟数组的大小为:26 * 26 * 26 * 10。
  2. 建立vector二维数组,存放每个同学的选课情况。 ​​vector<int> vi[100]​

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int N = 40010;
const int M = 26*26*26*10+1;
vector<int> stu[M];

//hash函数,将字符串name转为数字
int getId(string name){
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(int argc, char** argv) {

int stuNum = 0, courseNum = 0;
cin >> stuNum >> courseNum;
int course, x;

for(int i = 0; i < courseNum; i++){
cin >> course >> x;
// string name;
char name[5];
for(int j = 0; j < x; j++){
//getline(cin, name);
scanf("%s",name);
int id = getId(name);
stu[id].push_back(course);
}
}

for(int i = 0; i < stuNum; i++){
char name[5];
scanf("%s",name);
cout << name << " ";
int id = getId(name);
sort(stu[id].begin(), stu[id].end());
cout << stu[id].size();
for(int j = 0; j < stu[id].size(); j++){
cout << " " << stu[id][j];
}
cout << endl;
}

return 0;
}


举报

相关推荐

0 条评论