PAT DS 8 04 列印學生選課清單

2021-06-26 19:14:54 字數 1364 閱讀 3529

這道題乍一看應該hash一下就可以了,沒想到做著做著發現,要麼時間不夠,要麼空間不夠……

1. 先確定用40111這個素數做hash表,儲存所有學生選課資訊。

2. 選了哪些課的儲存需要細細想想。題目沒給選課量的上限,先前根據實際情況考慮不會超過50門,遺憾的出現段錯誤。於是改用鍊錶。

3. 題目沒說選課資訊會按照課程編號給出。所以還要對每個學生的選課鍊錶做排序。我第一次用陣列儲存鍊錶資訊下來,再做qsort,卻超時了。第二次改用,直接插入排序,ac了。由於某些原因,我的排序在輸出時做的,可以在處理選課資訊時(add函式裡)直接做。但應該從大端向小端掃瞄插入,需要用雙向鍊錶和乙個尾指標。

#include #include #include #include #include #define hash	40111

int n, k;

typedef struct s_coursecour;

typedef struct s_studentstud;

stud *st[hash];

int hash(char *name);

void add(char *name, int course);

void printc(char *name);

int main(void)

ps = ps->nx;

} // new

if((ps = (stud*)malloc(sizeof(stud))) == null)

exit(1);

strcpy(ps->na, na);

ps->co = (cour*)malloc(sizeof(cour));

ps->co->num = co;

ps->co->nx = null;

++ps->cnt;

if(pre != null)

pre->nx = ps;

else

st[h] = ps;

}void printc(char *na)

else

co[i] = co[i+1];

}#ifdef debug

printf("\n%d:", cnt);

for(i=cnt; icnt; ++i)

printf("-%d", co[i]);

#endif

pc = pc->nx;

}for(i=0; icnt; ++i)

printf(" %d", co[i]);

printf("\n");

break;

} ps = ps->nx;

} if(ps == null)

printf("%s 0\n", na);

}

8 04 列印學生選課清單

include include include includeusing namespace std char name 5 int f char p unordered multimapke int main for while n return 0 用雜湊表,但肯定不需要自己寫乙個雜湊表,自己再...

7 47 列印選課學生名單

7 47 列印選課學生名單 25 分 假設全校有最多40000名學生和最多2500門課程。現給出每個學生的選課清單,要求輸出每門課的選課學生名單。輸入的第一行是兩個正整數 n 40000 為全校學生總數 k 2500 為總課程數。此後n行,每行包括乙個學生姓名 3個大寫英文本母 1位數字 乙個正整數...

5 49 列印學生選課清單 25分

假設全校有最多40000名學生和最多2500門課程。現給出每門課的選課學生名單,要求輸出每個前來查詢的學生的選課清單。輸入的第一行是兩個正整數 n le 40000 為前來查詢課表的學生總數 k le 2500 為總課程數。此後順序給出課程1到k的選課學生名單。格式為 對每一門課,首先在一行中輸出課...