学生成绩排序 | |||
| |||
Description | |||
一个班有3门课和若干名学生,按学生的平均成绩由大到小输出学生成绩。 要求:必须定义相应的学生结构,并用学生结构变量表示学生。 | |||
Input | |||
有多组数据。 每组数据的第1个数是一个整数,表示学生数;其后是每一个学生的名字和3门课的成绩。 | |||
Output | |||
按平均成绩由大到小输出。 名字左对齐,成绩保留两位小数,成绩之间以空格分隔。 |
Sample Input |
3 zhang 70 80 90 wang 90 88 55 liu 75 80 60 2 zhao 77 88 52 xu 65 90 70 |
Sample Output |
zhang 70.00 80.00 90.00 80.00 wang 90.00 88.00 55.00 77.67 liu 75.00 80.00 60.00 71.67 xu 65.00 90.00 70.00 75.00 zhao 77.00 88.00 52.00 72.33 |
#include <bits/stdc++.h>
struct stud_score
{
char name[10];
double score[3];
double ave;
};
int main()
{
int N;
N++;
while(scanf("%d",&N)!=EOF)
{
struct stud_score student[N];
for(int i=0;i<N;i++)
{
scanf("%s %lf %lf %lf",&student[i].name,&student[i].score[0],&student[i].score[1],&student[i].score[2]);
student[i].ave=(student[i].score[0]+student[i].score[1]+student[i].score[2])/3;
}
int k,j;
struct stud_score temp;
for(k=0;k<N-1;k++)
for(j=k+1;j<N;j++)
{
if(student[k].ave<student[j].ave)
{
temp=student[k];
student[k]=student[j];
student[j]=temp;
}
}
for(int g=0;g<N;g++)printf("%s %.2f %.2f %.2f %.2f\n",student[g].name,student[g].score[0],student[g].score[1],student[g].score[2],student[g].ave);
}
}