题目原文
题目描述
编写函数Sort_by_score(),生成根据分数由高到低顺序排列学生信息的表格。在表格中生成新的一列,存放排名情况。参数列表不作限制。
输入
一系列学生信息
输出
排序后的新表格。分数相同的同学名次并列,按照学号顺序输出
样例
输入样例1
Jingyu LI 202100000000 85
Jy LEE 202100100000 89
Jxxxyx Leeeee 202000100000 100
Jingyu11 LI 202100000001 85
Jingyuuuu LI 202100000900 59
Jingyuuuu Lous 202000000900 49
Asss Dccc 202112344321 84
Asssd Dccec 202112244321 83
输出样例1
Name_f Name_l stu_id score Rank
Jxxxyx Leeeee 202000100000 100 1
Jy LEE 202100100000 89 2
Jingyu LI 202100000000 85 3
Jingyu11 LI 202100000001 85 3
Asss Dccc 202112344321 84 5
Asssd Dccec 202112244321 83 6
Jingyuuuu LI 202100000900 59 7
Jingyuuuu Lous 202000000900 49 8
AC代码
#include <stdio.h>
#include <string.h>
struct Information
{
char first_name[20];
char last_name[20];
char id[20];
int score;
int rank;
} stu[110], t;
void Sort_by_score(int n)
{
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (stu[i].score < stu[j].score)
{
t = stu[i];
stu[i] = stu[j];
stu[j] = t;
}
}
}
}
void Set_rank(int n)
{
stu[0].rank = 1;
for (int i = 1; i < n; i++)
{
if (stu[i].score == stu[i - 1].score)
stu[i].rank = stu[i - 1].rank;
else
stu[i].rank = i + 1;
}
}
int main()
{
printf("Name_f Name_l stu_id score Rank\n");
int n = 0;
while (~scanf("%s%s%s%d", stu[n].first_name, stu[n].last_name, stu[n].id, &stu[n].score))
n++;
Sort_by_score(n);
Set_rank(n);
for (int i = 0; i < n; i++)
printf("%s %s %s %d %d\n", stu[i].first_name, stu[i].last_name, stu[i].id, stu[i].score, stu[i].rank);
return 0;
}