A1047 Student List for Course (25 分)

一、技术总结

  1. 首先题目要看清湖,提出的条件很关键,比如for循环的终止条件,特别注意。
  2. 还有这个题目主要考虑到vector的使用,还有注意一定要加上using namespace std;
  3. 输出格式,题目中如果没有要求什么最后一行不能有空格什么的,就不要画蛇添足,按照正常的换行就行。
  4. 这里采用的是先使用二维字符串数组存储名字,然后名字也有编号,使用这个编号来记录每个课程中选取的学生,巧妙的避免了要记录下字符串的尴尬局面。然后再开辟一个vector容器来记录课程中选课学生的编号,最后排序即可。
  5. 如果很大的字符串存储,不要使用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;
}

猜你喜欢

转载自www.cnblogs.com/tsruixi/p/11915668.html