1752: 学生数据排序

#include<stdio.h>
struct student
{
char name[9];
long no;
int score[4];
};
void input(struct student stu[100],int n)
{
int i;
for(i=1;i<=n;i++)
{
scanf("%ld %s %d %d %d",&stu[i].no,&stu[i].name,&stu[i].score[1],&stu[i].score[2],&stu[i].score[3]);
stu[i].score[0]=stu[i].score[1]+stu[i].score[2]+stu[i].score[3];
}
return;
}
void sort(struct student stu[100],int n)
{
struct student t;
int i,j;
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
{
if(stu[i].score[0]<stu[j].score[0] || stu[i].score[0]==stu[j].score[0] && stu[i].no>stu[j].no)
{
t=stu[i];
stu[i]=stu[j];
stu[j]=t;
}
}
return;
}
void print(struct student stu[100],int n)
{
int i;
for(i=1;i<=n;i++)
{
printf("%d ",i);
printf("%ld %s %d %d %d %d\n",stu[i].no,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].score[3]);
}
return;
}
int main()
{
int n;
struct student stu[100];
while(scanf("%d",&n)!=EOF)
{
input(stu,n); //读入n个学生的数据
sort(stu,n); //按照总分降序排序,如果总分相同的学号小的在前面
print(stu,n); //输出n个学生的信息
}
return 0;
}

posted @ 2020-02-06 18:34  我是个好孩子  阅读(172)  评论(0编辑  收藏  举报