厦大C语言上机 1397 数据排序
1397.数据排序时间限制: 1000 MS 内存限制: 65536 K
提交数: 1268 (0 users) 通过数: 116 (115 users)
问题描述
按一定顺序给n个学生数据“学号语文成绩 数学成绩 英语成绩 物理成绩 化学成绩 生物成绩”,现在让你按其中某一成绩从大到小排序(询问1按语文成绩排序,询问2按数学成绩排序,询问3按英语成绩排序,询问4按物理成绩排序,询问5按化学成绩排序,询问6按生物成绩排序),若成绩相同学号小的排在前面。输出排序后每个同学之前在原数据中的位置(如第一个读入数据的同学为1,最后一个读入数据的同学为N)。
输入格式
输入的第一行为一个整数N,代表有N个学生数据。(n<=1000)
接下来每一行一次输入学号、语文成绩、数学成绩、英语成绩、物理成绩、化学成绩、生物成绩,其中学号是一个十四位整数(该数值超过int类型表示的最大范围,请使用字符数组比较或者long long类型),成绩是一个范围的整数。
再一行输入一个询问整数K∈{1,2,3,4,5,6},表示按第K种成绩排序,如题目所述。
输出格式
输出N行,代表排序后每个同学在原数据中所处的位置,即这个同学在原数据中是第几行。
样例输入
3
23020102203458 70 50 30 60 80 90
23020102203457 50 60 70 80 90 100
23020102203456 70 60 50 30 20 80
2
样例输出
3
2
1
来源
xmu
#include <stdio.h>
#include <string.h>
struct Student
{
char id;
int chinese;
int math;
int english;
int physics;
int chemistry;
int biology;
int index;
};
int cmp_chinese(struct Student a, struct Student b)
{
return (a.chinese < b.chinese) || ((a.chinese == b.chinese) && (strcmp(a.id, b.id) > 0));
}
int cmp_math(struct Student a, struct Student b)
{
return (a.math < b.math) || ((a.math == b.math) && (strcmp(a.id, b.id) > 0));
}
int cmp_english(struct Student a, struct Student b)
{
return (a.english < b.english) || ((a.english == b.english) && (strcmp(a.id, b.id) > 0));
}
int cmp_physics(struct Student a, struct Student b)
{
return (a.physics < b.physics) || ((a.physics == b.physics) && (strcmp(a.id, b.id) > 0));
}
int cmp_chemistry(struct Student a, struct Student b)
{
return (a.chemistry < b.chemistry) || ((a.chemistry == b.chemistry) && (strcmp(a.id, b.id) > 0));
}
int cmp_biology(struct Student a, struct Student b)
{
return (a.biology < b.biology) || ((a.biology == b.biology) && (strcmp(a.id, b.id) > 0));
}
int partition(struct Student *array, int low, int high, int (*cmp)(struct Student, struct Student))
{
int mid = (low + high) / 2;
array = array;
array = array;
while (low < high)
{
while (low < high && (*cmp)(array, array))
--high;
array = array;
while (low < high && !(*cmp)(array, array))
++low;
array = array;
}
array = array;
return low;
}
void quicksort(struct Student *array, int low, int high, int (*cmp)(struct Student, struct Student))
{
if (low >= high)
return;
int pivot = partition(array, low, high, cmp);
quicksort(array, low, pivot-1, cmp);
quicksort(array, pivot+1, high, cmp);
}
int main()
{
int n, k, i;
struct Student stu;
int (*cmp)(struct Student, struct Student);
scanf("%d", &n);
for (i = 1; i <= n; ++i)
{
scanf("%s %d %d %d %d %d %d", stu.id, &stu.chinese, &stu.math, &stu.english, &stu.physics,
&stu.chemistry, &stu.biology);
stu.index = i;
}
scanf("%d", &k);
switch (k)
{
case 1: cmp = cmp_chinese; break;
case 2: cmp = cmp_math; break;
case 3: cmp = cmp_english; break;
case 4: cmp = cmp_physics; break;
case 5: cmp = cmp_chemistry; break;
case 6: cmp = cmp_biology; break;
default: break;
}
quicksort(stu, 1, n, cmp);
for (i = 1; i <= n; ++i)
printf("%d\n", stu.index);
return 0;
}
文档来源:51CTO技术博客https://blog.51cto.com/liulizhi1996/3032793
页:
[1]