zoukankan      html  css  js  c++  java
  • PAT Advanced 1047 Student List for Course (25) [不定⻓数组vector,STL的使⽤]

    题目

    Zhejiang University has 40000 students and provides 2500 courses. Now given the registered course list of each student, you are supposed to output the student name lists of all the courses.
    Input Specification:
    Each input file contains one test case. For each case, the first line contains 2 numbers: N (<=40000), the total number of students, and K (<=2500), the total number of courses. Then N lines follow, each contains a student’s name (3 capital English letters plus a one-digit number), a positive number C (<=20) which is the number of courses that this student has registered, and then followed by C course numbers. For the sake of simplicity, the courses are numbered from 1 to K.
    Output Specification:
    For each test case, print the student name lists of all the courses in increasing order of the course numbers. For each course, first print in one line the course number and the number of registered students, separated by a space. Then output the students’ names in alphabetical order. Each name occupies a line.
    Sample Input:
    10 5
    ZOE1 2 4 5
    ANN0 3 5 2 1
    BOB5 5 3 4 2 1 5
    JOE4 1 2
    JAY9 4 1 2 5 4
    FRA8 3 4 2 5
    DON2 2 4 5
    AMY7 1 5
    KAT3 3 5 4 2
    LOR6 4 2 4 1 5
    Sample Output:
    1 4
    ANN0
    BOB5
    JAY9
    LOR6
    2 7
    ANN0
    BOB5
    FRA8
    JAY9
    JOE4
    KAT3
    LOR6
    3 1
    BOB5
    4 7
    BOB5
    DON2
    FRA8
    JAY9
    KAT3
    LOR6
    ZOE1
    5 9
    AMY7
    ANN0
    BOB5
    DON2
    FRA8
    JAY9
    KAT3
    LOR6
    ZOE1

    题目分析

    已知每个学生选课情况,列出每门课程已报名学生列表

    解题思路

    使用vector在录入信息时,保存每门课程的学生信息,最后排序打印即可

    知识点

    字符数组比较大小(注意strcmp除了返回-1,0,1还会)

    bool cmp(int a,int b){  
    	return strcmp(names[a],names[b])<0; // 字符数组比较
    }
    

    易错点

    字符串长度固定的情况,尽量使用字符数组处理,防止超时

    Code

    Code 01(string)

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    const int maxn=2510;
    vector<string> scl[maxn];
    int main(int argc,char * argv[]) {
    	int n,k,ct,c;
    	string s;
    	scanf("%d %d",&n,&k);
    	for(int i=0; i<n; i++) {
    		cin>>s>>ct;
    		for(int j=0; j<ct; j++) {
    			scanf("%d",&c);
    			scl[c].push_back(s);
    		}
    	}
    	for(int i=1; i<=k; i++) {
    		vector<string> names = scl[i];
    		sort(names.begin(),names.end());
    		printf("%d %d
    ",i,names.size());
    		for(int j=0; j<names.size(); j++) {
    			printf("%s
    ",names[j].c_str());
    		}
    	}
    	return 0;
    }
    

    Code 02 (char[])

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    const int maxcn=2510;
    const int maxsn=40010;
    vector<int> scl[maxcn];
    char names[maxsn][5];
    bool cmp(int a,int b){  
    	return strcmp(names[a],names[b])<0; // 字符数组比较
    }
    int main(int argc,char * argv[]) {
    	int n,k,ct,c;
    	scanf("%d %d",&n,&k);
    	for(int i=0; i<n; i++) {
    		scanf("%s %d",names[i],&ct);
    		for(int j=0; j<ct; j++) {
    			scanf("%d",&c);
    			scl[c].push_back(i);
    		}
    	}
    	for(int i=1; i<=k; i++) {
    		vector<int> cln = scl[i];
    		sort(cln.begin(),cln.end(),cmp);
    		printf("%d %d
    ",i,cln.size());
    		for(int j=0; j<cln.size(); j++) {
    			printf("%s
    ",names[cln[j]]);
    		}
    	}
    	return 0;
    }
    

  • 相关阅读:
    数据倾斜原理及解决方案
    删除emp_no重复的记录,只保留最小的id对应的记录
    理解HBase面向列存储
    给数据库用户授权(对象多为系统表,如dba可以查看的表)
    SpringBoot里的一些注解
    01背包
    【转】简说GNU, GCC and MinGW (Lu Hongling)
    费马小定理
    欧拉定理
    【转】C中的静态存储区和动态存储区
  • 原文地址:https://www.cnblogs.com/houzm/p/12445474.html
Copyright © 2011-2022 走看看