题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/0383714a1bb749499050d2e0610418b1?tpId=40&tqId=21333&rp=1&ru=/ta/kaoyan&qru=/ta/kaoyan&difficulty=&judgeStatus=&tags=/question-ranking
#include <stdio.h> #include <stdlib.h> typedef struct node{ char name[16]; int score; }node; void swap(node *a, node *b){ node temp = *a; *a = *b; *b = temp; } int main() { int a, b; while (scanf("%d %d", &a, &b) != EOF) { // 注意 while 处理多个 case // 64 位输出请用 printf("%lld") to node arr[a]; for(int i=0;i<a;i++){ scanf("%s %d", arr[i].name, &arr[i].score); } int index[a]; for (int k=0;k<a;k++){ index[k] = 0; } for(int i=1;i<a;i++){ for(int j=i;j>0;j--){ if(b == 0){ //降序 if (arr[j-1].score < arr[j].score){ swap(&arr[j], &arr[j-1]); } }else if (b == 1) { //升序 if (arr[j-1].score > arr[j].score){ swap(&arr[j], &arr[j-1]); } } } } for(int i=0;i<a;i++){ printf("%s %d\n", arr[i].name, arr[i].score); } // printf("%d\n", b); } return 0; }