计算总分

来源:互联网 发布:php aes cbc解密乱码 编辑:程序博客网 时间:2024/04/29 10:17

统计成绩的时候,一名同学的成绩包括数学,英语,总分,要让你按照总分对所有同学进行排名,这时候就要用到结构体排序。



#include<iostream>

#include<algorithm>
using namespace std;
struct student{
char name[20];
int Math;
int English;
int Score;
}st[5];
bool map(student A,student B)//写一个结构体排序的方法
{
return A.Score>B.Score;从大到小排序
}
int main()
{
int i;
for(i=0;i<5;i++)
{
cin>>st[i].name>>st[i].Math>>st[i].English;
st[i].Score=st[i].Math+st[i].English;
}
sort(st,st+5,map);
for(i=0;i<5;i++)
{
cout<<st[i].name<<endl;
}
return 0;
}
原创粉丝点击