A1047 Student List for Course (25 分)
一、技术总结
- 首先题目要看清湖,提出的条件很关键,比如for循环的终止条件,特别注意。
- 还有这个题目主要考虑到vector的使用,还有注意一定要加上using namespace std;
- 输出格式,题目中如果没有要求什么最后一行不能有空格什么的,就不要画蛇添足,按照正常的换行就行。
- 这里采用的是先使用二维字符串数组存储名字,然后名字也有编号,使用这个编号来记录每个课程中选取的学生,巧妙的避免了要记录下字符串的尴尬局面。然后再开辟一个vector容器来记录课程中选课学生的编号,最后排序即可。
- 如果很大的字符串存储,不要使用string,要使用char数组,可以是二维的。然后如果要比较字符串的大小,按字母从小到大使用cmp如下:
bool cmp(int a, int b){
//这里是按字符串从小到大,如果相反改变下面中小于号为大于号即可,同时stu是提前声明的全局变量二维字符串数组
return strcmp(stu[a], stu[b]) < 0;
}
二、参考代码
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 40010;
const int K = 2510;
char stu[N][5];
vector<int> q[K];
bool cmp(int a, int b){
return strcmp(stu[a], stu[b]) < 0;
}
int main(){
int n,k;
scanf("%d%d", &n, &k);
int num = 0;
int course = 0;
for(int i = 0; i < n; i++){
scanf("%s %d", stu[i], &num);
for(int j = 0; j < num; j++){
scanf("%d", &course);
q[course].push_back(i);
}
}
for(int i = 1; i <= k; i++){
sort(q[i].begin(), q[i].end(), cmp);
printf("%d %d\n", i, q[i].size());
for(int j = 0; j < q[i].size(); j++){
printf("%s\n", stu[q[i][j]]);
}
}
return 0;
}
A1047 Student List for Course (25 分)的更多相关文章
- PAT 甲级 1047 Student List for Course (25 分)(cout超时,string scanf printf注意点,字符串哈希反哈希)
1047 Student List for Course (25 分) Zhejiang University has 40,000 students and provides 2,500 cou ...
- 1047 Student List for Course (25分)
Zhejiang University has 40,000 students and provides 2,500 courses. Now given the registered course ...
- 【PAT甲级】1047 Student List for Course (25 分)
题意: 输入两个正整数N和K(N<=40000,K<=2500),接下来输入N行,每行包括一个学生的名字和所选课程的门数,接着输入每门所选课程的序号.输出每门课程有多少学生选择并按字典序输 ...
- PAT 甲级 1039 Course List for Student (25 分)(字符串哈希,优先队列,没想到是哈希)*
1039 Course List for Student (25 分) Zhejiang University has 40000 students and provides 2500 cours ...
- PAT 甲级 1083 List Grades (25 分)
1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed ...
- 1109 Group Photo (25 分)
1109 Group Photo (25 分) Formation is very important when taking a group photo. Given the rules of fo ...
- 1012 The Best Rank (25 分)
1012 The Best Rank (25 分) To evaluate the performance of our first year CS majored students, we cons ...
- 1036 Boys vs Girls (25 分)
1036 Boys vs Girls (25 分) This time you are asked to tell the difference between the lowest grade of ...
- A1083 List Grades (25)(25 分)
A1083 List Grades (25)(25 分) Given a list of N student records with name, ID and grade. You are supp ...
随机推荐
- Centos 6 变更 窗口管理器
/etc/sysconfig/desktop ( 没有的话创建一个) DESKTOP="KDE" DISPLAYMANAGER="KDE"
- EF Core 3.0 Preview 9 的2个小坑
之前我们的数据库服务器使用的是 SQL Server 2008 R2 ,由于从 EF Core 3.0 Preview 6 开始不支持 UseRowNumberForPaging ,只能停留在 EF ...
- 蓝牙Inquriy 过程详解
问题 今天遇到了一个问题,就是自己的耳机产品,手机经常搜不到,从日志里面查看,发现原因是平时手机蓝牙发送的是inquiry mode =2 出问题的时候,inquiry mode =1 由于本设备很多 ...
- 打造IP代理池,Python爬取Boss直聘,帮你获取全国各类职业薪酬榜
爬虫面临的问题 不再是单纯的数据一把抓 多数的网站还是请求来了,一把将所有数据塞进去返回,但现在更多的网站使用数据的异步加载,爬虫不再像之前那么方便 很多人说js异步加载与数据解析,爬虫可以做到啊,恩 ...
- SpringBoot系列之@PropertySource用法简介
SpringBoot系列之@PropertySource用法简介 继上篇博客:SpringBoot系列之@Value和@ConfigurationProperties用法对比之后,本博客继续介绍一下@ ...
- Vue自定义指令使用方法详解 和 使用场景
Vue自定义指令的使用,具体内容如下 1.自定义指令的语法 Vue自定义指令语法如下: Vue.directive(id, definition) 传入的两个参数,id是指指令ID,definitio ...
- SqlServer ----- 根据查询语句创建视图
我们都知道视图的本质就是查询语句,那么就可以根据查询语句创建视图, 前提 知道视图的组成,已经写好的sql 语句,多表或单表的查询语句,将查询语句变成视图. 所以视图可以由单表,多表或视图加表构成. ...
- js使用“toFixed( )”保留小数点后两位
例如: var a = 1.335; alert(a.toFixed(2)) // IE 1.34 //chorme 1.33 若a为字符串,则需要先转换为Number类型 如: n = Number ...
- Springboot 打包自带启动脚本
一直以来,我都是 gradlew build java -jar xxx.jar 来启动springboot项目的.今天突然发现,springboot自动封装了一个bootJar的任务脚本. demo ...
- 使用C#+Edge (Chromium)进行Web自动化测试
今天看到了VisualStudio中现在已经自带了Web单元测试项目模板,便试了一下,发现还比较好用,它默认的是Selenium实现的,测试组在用Selenium+Python来写过自动化测试,原来它 ...