源代碼:https://github.com/yunwei37/myClassNotes
還有不少數據結構和算法相關的筆記以及pta題解哦x
假設全校有最多40000名學生和最多2500門課程。現給出每個學生的選課清單,要求輸出每門課的選課學生名單。
輸入格式:
輸入的第一行是兩個正整數:N(≤40000),爲全校學生總數;K(≤2500),爲總課程數。此後N行,每行包括一個學生姓名(3個大寫英文字母+1位數字)、一個正整數C(≤20)代表該生所選的課程門數、隨後是C個課程編號。簡單起見,課程從1到K編號。
輸出格式:
順序輸出課程1到K的選課學生名單。格式爲:對每一門課,首先在一行中輸出課程編號和選課學生總數(之間用空格分隔),之後在第二行按字典序輸出學生名單,每個學生名字佔一行。
輸入樣例:
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<list>
#include<vector>
using namespace std;
struct student{
char name[5];
int* c;//保存每個學生的選課;
}stu[40001];//對頭(學生名字)排序;
int cla[40001][20]={0};//每個學生的課程;
int count[2501]={0};//每門課程的人數;
int cmp(const void *a,const void *b){
return strcmp(((struct student*)a)->name,((struct student*)b)->name);
}
int main(){
int n,k;
scanf("%d %d\n",&n,&k);
int i,x,j,c,m,t;
char name[5];
vector<list<int> > a(k+1);
for(i=0;i<n;i++){
scanf("%s %d",name,&x);
strcpy(stu[i].name,name);
stu[i].c=cla[i];
for(j=0;j<x;j++){
scanf("%d",&t);
cla[i][j]=t;
count[t]++;
}
}
qsort(stu,n,sizeof(struct student),cmp);
for(i=0;i<n;i++){
for(j=0;j<20;j++)
if(stu[i].c[j]){
a[stu[i].c[j]].push_back(i);
}//轉到每個課程的鏈表;
}
for(i=1;i<=k;i++){
printf("%d %d\n",i,count[i]);
list<int>::iterator a1;
for(a1=a[i].begin();a1!=a[i].end();++a1){
printf("%s\n",stu[*a1].name);
}
}
return 0;
}