mirror of
https://github.com/renbaoshuo/202401-programming-assignments.git
synced 2024-12-16 15:44:39 +00:00
73 lines
1.6 KiB
Markdown
73 lines
1.6 KiB
Markdown
|
# 6-4 结构体数组按总分排序
|
||
|
|
||
|
有一组学生数据,每个数据中含有三门课成绩,请按成绩总和从高到低对这组数据进行排序。
|
||
|
编写函数calc求出每名学生的总分。
|
||
|
编写函数sort按每名学生的总分从高到低对这组数据进行排序
|
||
|
|
||
|
### 函数接口定义:
|
||
|
```c++
|
||
|
void calc(struct student *p,int n);
|
||
|
void sort(struct student *p,int n);
|
||
|
```
|
||
|
|
||
|
其中 `p` 和 `n` 都是用户传入的参数。
|
||
|
函数calc求出`p`指针所指的结构体数组中 `n` 名学生各自的总分。
|
||
|
函数sort对`p`指针所指的结构体数组的学生数据按总分降序排序。
|
||
|
|
||
|
### 裁判测试程序样例:
|
||
|
```c++
|
||
|
#include <stdio.h>
|
||
|
struct student
|
||
|
{
|
||
|
int num;
|
||
|
char name[15];
|
||
|
float score[3];
|
||
|
float sum;
|
||
|
};
|
||
|
void calc(struct student *p,int n);
|
||
|
void sort(struct student *p,int n);
|
||
|
int main()
|
||
|
{
|
||
|
struct student stu[5];
|
||
|
int i,j;
|
||
|
float f;
|
||
|
for(i=0;i<5;i++)
|
||
|
{
|
||
|
scanf("%d%s",&stu[i].num,stu[i].name);
|
||
|
for(j=0;j<3;j++)
|
||
|
{
|
||
|
scanf("%f",&f);
|
||
|
stu[i].score[j]=f;
|
||
|
}
|
||
|
}
|
||
|
calc(stu,5);
|
||
|
sort(stu,5);
|
||
|
for(i=0;i<5;i++)
|
||
|
{
|
||
|
printf("%5d%15s",stu[i].num,stu[i].name);
|
||
|
printf(" %.1f %.1f %.1f %.1f\n",stu[i].score[0],stu[i].score[1],stu[i].score[2], stu[i].sum);
|
||
|
}
|
||
|
return 0;
|
||
|
|
||
|
|
||
|
/* 请在这里填写答案 */
|
||
|
```
|
||
|
|
||
|
### 输入样例:
|
||
|
```in
|
||
|
1 zhang 89 87 85
|
||
|
2 liu 92 98 96
|
||
|
3 li 74 71 72
|
||
|
4 xion 95 98 99
|
||
|
5 liu 99 100 100
|
||
|
```
|
||
|
|
||
|
### 输出样例:
|
||
|
```out
|
||
|
5 liu 99.0 100.0 100.0 299.0
|
||
|
4 xion 95.0 98.0 99.0 292.0
|
||
|
2 liu 92.0 98.0 96.0 286.0
|
||
|
1 zhang 89.0 87.0 85.0 261.0
|
||
|
3 li 74.0 71.0 72.0 217.0
|
||
|
```
|