C ++ STL vector A1047 Student List for Course (25) (note string of storage: with char [N] [5] stored)

Did not think a two-dimensional array of char [N] [5] Enter the name of the store, try it yourself with a vector <char> failed.

Tip: If the direct sort strings, it will cause a lot of strings when sorting movement, very time-consuming, and therefore more appropriate approach is to use the index of the string to replace the string itself to sort this time will be consumed much less

strcmp necessarily return value is -1, 0, +1, there may be other positive and negative numbers. Can not be written at the time of writing cmp strcmp function returns a value equal to -1, must write <0 

 

 

#include <bits / STDC ++ H.> 
#include <math.h> 
#include < String >
 a using  namespace std;
 const  int MAXN = 40010 ; // maximum number of students 
const  int MAXC = 2510 ; // maximum number of courses door 
char name [MAXN] [ . 5 ]; // MAXN student 
Vector < int > Course [MAXC]; // Course [i] storing the course of all the i-th student number 
BOOL CMP ( int a, int B) {
     return strcmp (name [A], name [B]) < 0; 
} 
Int main () {
     int n-, K, C, courseID; 
    Scanf ( " % D% D " , & n-, & K); // number of students and Lessons 
    for ( int I = 0 ; I <n-; ++ I) { 
        Scanf ( " % S% D " , name [I], & C); // student name and number elective 
        for ( int J = 0 ; J <C; ++ J) { 
            Scanf ( " % D " , & courseID); 
            Course [courseID] .push_back (i); // students i is added in the course of courseID 
        } 
    } 
    for(int i = 1;i<=k;++i){
        printf("%d %d\n",i,course[i].size());//第i门课的学生数
        sort(course[i].begin(),course[i].end(),cmp);
        for(int j =0;j<course[i].size();++j){
            printf("%s\n",name[course[i][j]]);
        }
    }
    system("pause");
    return 0;
} 

Guess you like

Origin www.cnblogs.com/JasonPeng1/p/12199292.html