PAT

xiaoxiao2025-09-03  229

A1039studentlistforcourse 原题: 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Zhejiang University has 40000 students and provides 2500 courses. Now given the student name lists of all the courses, you are supposed to output the registered course list for each student who comes for a query.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers: N (<=40000), the number of students who look for their course lists, and K (<=2500), the total number of courses. Then the student name lists are given for the courses (numbered from 1 to K) in the following format: for each course i, first the course index i and the number of registered students Ni (<= 200) are given in a line. Then in the next line, Ni student names are given. A student name consists of 3 capital English letters plus a one-digit number. Finally the last line contains the N names of students who come for a query. All the names and numbers in a line are separated by a space.

Output Specification:

For each test case, print your results in N lines. Each line corresponds to one student, in the following format: first print the student’s name, then the total number of registered courses of that student, and finally the indices of the courses in increasing order. The query results must be printed in the same order as input. All the data in a line must be separated by a space, with no extra space at the end of the line.

Sample Input: 11 5 4 7 BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1 1 4 ANN0 BOB5 JAY9 LOR6 2 7 ANN0 BOB5 FRA8 JAY9 JOE4 KAT3 LOR6 3 1 BOB5 5 9 AMY7 ANN0 BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1 ZOE1 ANN0 BOB5 JOE4 JAY9 FRA8 DON2 AMY7 KAT3 LOR6 NON9 Sample Output: ZOE1 2 4 5 ANN0 3 1 2 5 BOB5 5 1 2 3 4 5 JOE4 1 2 JAY9 4 1 2 4 5 FRA8 3 2 4 5 DON2 2 4 5 AMY7 1 5 KAT3 3 2 4 5 LOR6 4 1 2 4 5 题目大意: 给出每个学生选课表,列出每个课程学生表。

#include<cstdio> #include<vector> #include<string.h> #include<algorithm> #include<iostream> using namespace std; /*typedef struct student { string name; vector <int> courses;//存学生修的课程 int count; int stuid; }str; typedef struct course { int courseid;//课程id int regnum;//订课人数 vector <string> stuname; vector <int> stuid; }cs; int main() { int stunum,counum; string tempname; scanf("%d %d",&stunum,&counum); struct course *c=new cs[counum];// struct student *p=new str[stunum]; for(int i=0;i<counum;i++) { scanf("%d %d",&c[i].courseid,&c[i].regnum); //for (vector<int>::iterator it =c[i].stuname.begin();it!=c[i].stuname.end();it++) vector<string>::iterator it =c[i].stuname.begin(); for(int j=0;j<c[i].regnum;j++) { scanf("%s",tempname); c[i].stuname.push_back(tempname); } while(scanf("%s",&tempname)) { c[i].stuname.push_back(tempname); } for (vector <string>::iterator it =c[i].stuname.begin();it!=c[i].stuname.end();it++) { printf("%s",*it); } } for(int k=0;k<stunum;k++) { scanf("%s",p[k].name); } }*/ const int n=40010;//总人数 const int m=26*26*26*10+1;//课程号 vector <int> selectedcos[m]; int turnint(char name[]) { int n=0; for(int i=0;i<3;i++) { n=26*n+(name[i]-'A'); } n=n*10+(name[3]-'0'); return n; } int main() { char name[5]; int studentnum,coursenum; scanf("%d %d",&studentnum,&coursenum); int cos,sltstunum; for (int j=0;j<coursenum;j++) { scanf("%d %d",&cos,&sltstunum); for(int k=0;k<sltstunum;k++) { scanf("%s",name); int y=turnint(name); selectedcos[y].push_back(cos); } } for(int t=0;t<studentnum;t++) { scanf("%s",name); cos=turnint(name); sort(selectedcos[cos].begin(),selectedcos[cos].end()); printf("%s %d",name,selectedcos[cos].size()); for(int r=0;r<selectedcos[cos].size();r++) { printf(" %d",selectedcos[cos][r]); } printf("\n"); } }

A1047courselistforstudent 原题: 时间限制 400 ms 内存限制 64000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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

题目大意: 给出每门课程的学生表,列出每个学生的选课表。

char name[stu][5]; bool cmp(int a,int b) { return strcmp(name[a],name[b])<0; }; int main() { vector <int> selectedcourse[2510]; int studentnum,coursenum; scanf("%d %d",&studentnum,&coursenum); int cosnum,seltemp; for(int i=0;i<studentnum;i++) { scanf("%s %d",&name[i],&cosnum); for(int j=0;j<cosnum;j++) { scanf("%d",&seltemp); selectedcourse[seltemp].push_back(i); } } for(int k=1;k<coursenum+1;k++) { printf("%d %d",k,selectedcourse[k].size()); printf("\n"); sort(selectedcourse[k].begin(),selectedcourse[k].end(),cmp); for(int t=0;t<selectedcourse[k].size();t++) { printf("%s\n",name[selectedcourse[k][t]]); } } }

两道题目正好是正反思维,其实用的是一个套路,就是要把以主键为关系的两个事物存在一个大的数组里,这样就可以按要求输出。

转载请注明原文地址: https://www.6miu.com/read-5035685.html

最新回复(0)