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;
    }
    

  • 相关阅读:
    POJ1737
    USACOTrainning.Cow Pedigrees
    USACOTrainning.Controlling Companies
    对象之间的关系有哪些,如何使用面向对象语言表达
    面向对象思想
    几种搜索引擎算法研究
    jQuery性能优化手册
    生成随机数的一个可靠算法,高质量的均匀分布的随机函数
    浮广告,兼容ie、firefox,多个漂浮不冲突,调用只需两行代码
    C#相关算法
  • 原文地址:https://www.cnblogs.com/houzm/p/12445474.html
Copyright © 2011-2022 走看看