题解 第三章排序与查找| #成绩排序#
成绩排序
http://www.nowcoder.com/practice/0383714a1bb749499050d2e0610418b1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXNAME 30
typedef struct Grade
{
char s[MAXNAME];
int p;
} Grade;
// http://c.biancheng.net/view/232.html
// https://www.cnblogs.com/flipped/p/5005627.html
// https://blog.csdn.net/u010968153/article/details/52807435
int cmp1(const void *a, const void *b)
{
return (*(Grade *)a).p > (*(Grade *)b).p;
}
int cmp0(const void *a, const void *b)
{
return (*(Grade *)a).p < (*(Grade *)b).p;
}
int main()
{
int Num, Flag;
while (scanf("%d%d", &Num, &Flag) != EOF)
{
Grade G[Num];
for (int i = 0; i < Num; i++)
scanf("%s%d", G[i].s, &G[i].p);
//以上即将所有数据录入
if (Flag)
qsort(G, Num, sizeof(Grade), cmp1);
else
qsort(G, Num, sizeof(Grade), cmp0);
//排序过程
for (int i = 0; i < Num; i++)
{
printf("%s %d\n", G[i].s, G[i].p);
}
}
return 0;
}
王道机试指南刷题 文章被收录于专栏
计划刷完这本书