题目描述
有一学生成绩表,包括学号、姓名、3门课程成绩。请按要求排序输出:若输入1,则按第1门课成绩降序输出成绩表,若输入为i(1<=i<=3),则按第i门课成绩降序输出成绩表。
输入
首先输入一个整数n(1<=n<=100),表示学生人数;
然后输入n行,每行包含一个学生的信息:学号(12位)、姓名(不含空格且不超过20位),以及3个整数,表示3门课成绩,数据之间用空格隔开。
最后一行输入一个整数i,表示要求按第i门课成绩降序排序输出,若该门课成绩相同,则按学号升序。
输出
输出按第i门课降序排序的结果,格式见样例。
样例输入
3
541207010188 Zhangling 89 78 95
541207010189 Wangli 85 87 99
541207010190 Fangfang 85 68 76
1
样例输出
541207010188 Zhangling 89 78 95
541207010189 Wangli 85 87 99
541207010190 Fangfang 85 68 76
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
typedef struct student
{
char ID[13];//字符串一般要定义大一些,因为字符串需要多存储一个'\0',否则会有意想不到的错误;
char name[25];
int score[3];
} STU;
int main()
{
int n,m;
STU p[110],temp;
scanf("%d",&n);
int i,j;
for(i=0; i<n; i++)
{
scanf("%s",p[i].ID);
scanf("%s",p[i].name);
for(j=0; j<3; j++)
{
scanf("%d",&p[i].score[j]);
}
}
scanf("%d",&m);
for(i=0; i<n-1; i++)
{
for(j=i+1; j<n; j++)
{
if(p[i].score[m-1]<p[j].score[m-1])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
else if(p[i].score[m-1]==p[j].score[m-1])
{
if(strcmp(p[i].ID,p[j].ID)>0)
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
}
for(i=0; i<n; i++)
{
printf("%s %s",p[i].ID,p[i].name);
for(j=0; j<3; j++)
{
printf(" %d",p[i].score[j]);
}
printf("\n");
}
return 0;
}