思路:
定义一个考生类,包含了学号,德分和才分。然后创建四个容器,按照四个条件判断是否符合,符合的加入到容器中。然后我们要给容器排序,按照我们的输出要求给排序。
需要掌握的点:
1,泛型算法中的sort函数
2,vector容器
代码:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class student {
friend void operator>>(istream& cin, student& stu);
public:
student();
int getNum();
int getdScore();
int getcScore();
private:
int _number;
int _dScore;
int _cScore;
};
student::student() {
_number = 0;
_dScore = 0;
_cScore = 0;
}
int student::getNum()
{
return _number;
}
int student::getdScore()
{
return _dScore;
}
int student::getcScore()
{
return _cScore;
}
void operator>>(istream& cin,student& stu){
cin >> stu._number >> stu._dScore >> stu._cScore;
}
bool cmp(student stu1, student stu2) {
if (stu1.getcScore() + stu1.getdScore() > stu2.getcScore() + stu2.getdScore()) {
return true;
}else if((stu1.getcScore() + stu1.getdScore() == stu2.getcScore() + stu2.getdScore()) && stu1.getdScore()>stu2.getdScore()){
return true;
}
else if (stu1.getcScore() + stu1.getdScore() == stu2.getcScore() + stu2.getdScore() && stu1.getdScore() == stu2.getdScore() && stu1.getNum() < stu2.getNum()) {
return true;
}
return false;
}
void output(vector<student> &svec, ostream &cout) {
for (int i = 0; i < svec.size(); i++) {
cout << svec[i].getNum() << " " << svec[i].getdScore() << " " << svec[i].getcScore() << endl;;
}
}
int main() {
int N, L, H;
cin >> N >> L >> H;
int lastNum = 0;
student* stu = new student[N];
for (int i = 0; i < N; i++) {
cin >> stu[i];
if (stu[i].getcScore() >= L && stu[i].getdScore() >= L) {
lastNum++;
}
}
vector<student> svec1;
vector<student> svec2;
vector<student> svec3;
vector<student> svec4;
for (int i = 0; i < N; i++) {
if (stu[i].getcScore() >=H && stu[i].getdScore() >= H) {
//德分和才分都到线
svec1.push_back(stu[i]);
}
else if (stu[i].getcScore() < H && stu[i].getcScore() >= L && stu[i].getdScore() >= H) {
//德分到线,才分不到线
svec2.push_back(stu[i]);
}
else if (stu[i].getcScore() < H && stu[i].getdScore() < H && stu[i].getcScore() >= L && stu[i].getdScore() >= L && stu[i].getdScore() >= stu[i].getcScore()) {
svec3.push_back(stu[i]);
}
else if (stu[i].getcScore() >= L && stu[i].getdScore() >= L) {
svec4.push_back(stu[i]);
}
}
sort(svec1.begin(), svec1.end(), cmp);
sort(svec2.begin(), svec2.end(), cmp);
sort(svec3.begin(), svec3.end(), cmp);
sort(svec4.begin(), svec4.end(), cmp);
cout << lastNum<<endl;
output(svec1, cout);
output(svec2, cout);
output(svec3, cout);
output(svec4, cout);
}