题目描述
已知有3名学生及五门课程的成绩,要求根据学生的各科平均分排序(降序),并输出学生的所有信息和平均分(用指针数组完成)。
隐含条件:小数部分为零则不保留小数
输入样例
Jane 90 80 75 60 85
Mark 85 78 98 85 86
Lily 56 65 75 68 80
输出样例
Mark 85 78 98 85 86 86.4
Jane 90 80 75 60 85 78
Lily 56 65 75 68 80 68.8
方案1
#include<bits/stdc++.h>
using namespace std;
struct student{
double b[5],ave=0.0;
string name;
}a[3];
bool cmp(student c,student d) {return c.ave>d.ave;}
int main(){
string s;
for(int t=0;t<3;t++){
student *stu=&a[t];
cin>>stu->name;
for(int j=0;j<5;j++){
cin>>stu->b[j];
stu->ave+=stu->b[j];
}
stu->ave/=5.0;
}
sort(a,a+3,cmp);
for(int t=0;t<3;t++){
student *stu=&a[t];
cout<<stu->name;
double *p=stu->b;
for(int j=0;j<5;j++)cout<<" "<<(int)*p++;
if(int(stu->ave*10)%10==0)printf(" %d\n",stu->ave);//无小数位
else printf(" %.1f\n",stu->ave);
}
}