← C 語言
第 41 課,共 77 課 · 10 XP
結構陣列
理論與題目說明
機器翻譯,尚未經母語人士審閱。
結構可以放進陣列——在 C 裡,資料表就是這樣儲存的:
struct student {
char name[32];
int score;
};
struct student group[30];
group[0].score = 95;
strcpy(group[0].name, "Kai");
常見的題目是找出值最好的那個元素:
int best = 0;
for (int i = 1; i < n; i++)
if (group[i].score > group[best].score)
best = i;
printf("%s\n", group[best].name);
補充:不要每一步都複製結構,而是記住最好那個的索引。
題目。 讀入 n(最多 50)和 n 行 name score 格式的資料。顯示最好的學生的名字和分數。最好的有好幾個人時,顯示其中第一個。