题干
输入样例:
5
zhaoyi 70 80 90 240
qianer 65 32 77 174
sunsan 100 55 68 223
lisi 86 77 90 253
wangwu 100 59 66 225
输出样例:
*[qianer] 65 32 77
*[sunsan] 100 55 68
*[wangwu] 100 59 66
lisi 86 77 90
zhaoyi 70 80 90
wangwu 100 59 66
sunsan 100 55 68
qianer 65 32 77
C++实现
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct student {
char name[10];
int one; //第一科成绩
int two;
int three;
int total; //总成绩
bool flag;//是否挂科
};
bool cmp(student a, student b) {
都是3科,平均成绩越高总成绩就越高,所以只需比较总成绩
return a.total > b.total;
}
int main() {
int N;
//读入学生信息
while (cin >> N) {
vector<student> s;
for (int i = 0; i < N; i++)
{
student t;
cin >> t.name >> t.one >> t.two >> t.three >> t.total;
if (t.one < 60 || t.two < 60 || t.three < 60) {
t.flag = false;//挂科则为false
}
else {
t.flag = true;
}
s.push_back(t);
}
//如果挂科了
for (int i = 0; i < s.size(); i++)
{
if (!s[i].flag) {
cout << "*[" << s[i].name << "]";
cout << " " << s[i].one << " " << s[i].two << " " << s[i].three << endl;
}
}
sort(s.begin(), s.end(), cmp);
for (int i = 0; i < s.size(); i++)
{
cout << s[i].name;
cout << " " << s[i].one << " " << s[i].two << " " << s[i].three << endl;
}
}
return 0;
}