题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { char name[20]; int score; }stu_t; typedef struct Stu_Link_TypeDef { stu_t stu; struct Stu_Link_TypeDef* next; }stu_link_t; void print_link(stu_link_t* head) { stu_link_t* temp = head; while(temp) { printf("%s %d\n",temp->stu.name,temp->stu.score); temp = temp->next; } } int main() { int num, mode; stu_link_t* link_set[101] = {0}; while(scanf("%d%d", &num, &mode) != EOF) { for(int i = 0; i < num; i++) { stu_link_t* new = (stu_link_t*)malloc(sizeof(stu_link_t)); memset(new,0,sizeof(stu_link_t)); scanf("%s", new->stu.name); scanf("%d", &new->stu.score); stu_link_t* link_temp = link_set[new->stu.score]; if(!link_temp) link_set[new->stu.score] = new; else { for(;link_temp->next!=NULL;link_temp = link_temp->next); link_temp->next = new; } } if(mode == 1) { for(int j = 0;j<=100;j++) { if(!link_set[j]) continue; print_link(link_set[j]); } } else if(!mode) { for(int j = 100;j>=0;j--) { if(!link_set[j]) continue; print_link(link_set[j]); } } } return 0; }