博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[编程题]Student List for Course (25)
阅读量:359 次
发布时间:2019-03-04

本文共 973 字,大约阅读时间需要 3 分钟。

关键词:vector , map

试题链接:
https://www.nowcoder.com/questionTerminal/e9482512572944199d1babbcdf567b3d
问题描述:在这里插入图片描述
在这里插入图片描述
思路:
这道题的核心就是变长数组vector的应用(以及map的应用),做题之前应当好好考虑一下vector该用在存储什么变量是最合适的,考虑它“长度可变”的特性让自己结题的过程变得更加简单。

Course List for Student(https://blog.csdn.net/qq_45642765/article/details/116152924)

的基础上稍微做一点修改即可。

备注:

map迭代器访问元素使用时容易出现一些错误,iter->second指向的就是当前iter指针指向的map元素的值,不要和数组形访问map元素混淆了。

解决方案:

#include
#include
#include
#include
#include
using namespace std;int main(){
int courseNum; int studentNum; map
> stumap; int course,num; char name[5]; cin>>studentNum>>courseNum; for(int i=0;i
>course>>num; //课程号和选该课程的学生人数 //第一次出现该学生的名字 for(int j=0;j
>name; if(stumap.count(name)==0){ vector
list; list.push_back(course); stumap[name]=list; //非第一次出现学生名字 }else{ stumap[name].push_back(course); } } } //学生顺序 for(int i=0;i
>name; cout<
<<" "<
<<" "; //给课程号排序 sort(stumap[name].begin(),stumap[name].end()); for(int j=0;j
你可能感兴趣的文章