2308 - Student List for Course (25)
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.
题目输入
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.
题目输出
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.
输入/输出样例
输入格式
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
输出格式
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
C语言解答
#include<stdio.h> #include<string.h> #include <stdlib.h> #define MaxName 4 #define MaxC 20 typedef struct ListNode *List; struct ListNode { char Name[MaxName+1]; List Next; }; struct StudentNode { char Name[MaxName+1]; int nC; int C[MaxC]; } *Student; struct CourseNode { int Counter; List Ptr; } *Course; int CmpName(const void *a, const void *b) { return strcmp(((const struct StudentNode*)a)->Name, ((const struct StudentNode*)b)->Name); } void Read_and_Sort( int *N, int *K ) { int i, j; scanf("%d %d\n", N, K); Student = malloc( sizeof( struct StudentNode ) * (*N) ); Course = malloc( sizeof( struct CourseNode ) * (*K) ); for (i=0; i<(*K); i++) { Course[i].Counter = 0; Course[i].Ptr = NULL; } for (i=0; i<(*N); i++) { scanf("%s %d", Student[i].Name, &Student[i].nC); for (j=0; j<Student[i].nC; j++) scanf("%d", &Student[i].C[j]); } qsort(Student, (*N), sizeof(struct StudentNode), CmpName); } List NewNode( char *name ) { List temp; temp = (List)malloc(sizeof(struct ListNode)); strcpy(temp->Name, name); temp->Next = NULL; return temp; } void InsertCourse( int N, int K ) { List Node; int i, j, CourseIndex; for (i=N-1; i>=0; i--) for (j=Student[i].nC-1; j>=0; j--) { CourseIndex = Student[i].C[j]-1; Node = NewNode(Student[i].Name); Node->Next = Course[CourseIndex].Ptr; Course[CourseIndex].Ptr = Node; Course[CourseIndex].Counter ++; } } void Output( int K ) { List Ptr; int i; for (i=0; i<K; i++) { printf("%d %d\n", i+1, Course[i].Counter); for (Ptr = Course[i].Ptr; Ptr; Ptr = Ptr->Next) printf("%s\n", Ptr->Name); } } int main() { int N, K; Read_and_Sort( &N, &K ); InsertCourse( N, K ); Output( K ); return 0; }
C++解答
#include<cstdio> #include<algorithm> #include<vector> using namespace std; const int MAX=2510; vector<int> course[MAX]; //string to number int S2N(char str []) { int sum=0; for(int i=0;i<3;i++) { sum=sum*26+str[i]-'A'; } sum=sum*10+str[3]-'0'; return sum; } void N2S(int num,char str[]) { str[4]='\0'; str[3]=num%10+'0'; num/=10; for(int i=2;i>=0;i--) { str[i]=num%26+'A'; num/=26; } } int main(int argc, char *argv[]) { int N,M; scanf("%d%d",&N,&M); for(int i=0;i<N;i++) { char temp[6]; scanf("%s",temp); int index=S2N(temp); int count; scanf("%d",&count); for(int j=0;j<count;j++) { int num; scanf("%d",&num); course[num].push_back(index); } } for(int i=1;i<=M;i++) { sort(course[i].begin(),course[i].end()); printf("%d %d\n",i,course[i].size()); for(int j=0;j<course[i].size();j++) { char temp[6]; N2S(course[i][j],temp); printf("%s\n",temp); } } return 0; }