-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuawei3.cpp
143 lines (121 loc) · 3.33 KB
/
huawei3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
// Representation of a student.
class Student
{
public:
Student(const string& name, int ch, int ma, int en):
name_(name), chinese_(ch), math_(ma), english_(en)
{}
~Student() = default;
friend ostream& operator<<(ostream&, const Student&);
friend bool operator<(const Student&, const Student&);
friend bool operator==(const Student&, const Student&);
public:
// implicit inline
int TotalScore() const
{ return chinese_ + math_ + english_; }
// selectors
string name() const { return name_; }
int chinese() const { return chinese_; }
int math() const { return math_; }
int english() const { return english_; }
private:
string name_;
int chinese_;
int math_;
int english_;
};
// Compare policy.
bool operator<(const Student& lhs, const Student& rhs)
{
if (lhs.TotalScore() > rhs.TotalScore())
return true;
else if (lhs.TotalScore() < rhs.TotalScore())
return false;
if (lhs.chinese_ > rhs.chinese_)
return true;
else if (lhs.chinese_ < rhs.chinese_)
return false;
if (lhs.math_ > rhs.math_)
return true;
else if (lhs.math_ < rhs.math_)
return false;
// if (lhs.english_ > rhs.english_)
// return true;
// else if (lhs.english_ < rhs.english_)
// return false;
// now the two students have exactly same scores
if (lhs.name_ < rhs.name_)
return true;
return false;
}
bool operator==(const Student& lhs, const Student& rhs)
{
return lhs.TotalScore() == rhs.TotalScore()
&& lhs.chinese_ == rhs.chinese_
&& lhs.math_ == rhs.math_;
}
bool operator!=(const Student& lhs, const Student& rhs)
{ return !(lhs == rhs); }
// Print function.
ostream& operator<<(ostream& os, const Student& st)
{
os << st.name_ << ' '
<< st.chinese_ << ' '
<< st.math_ << ' '
<< st.english_;
return os;
}
// Filter unpassed stduents.
void FilterPassed(vector<Student>& students)
{
// remove unpassed
auto it = std::remove_if(students.begin(), students.end(),
[](const Student& st)
{
if (st.chinese() < 60 ||
st.math() < 60 ||
st.english() < 60)
{
return true;
}
if (st.name().size() > 10)
return true;
return false;
});
// remove the tail
int remain = students.end() - it;
for (int i = 0; i < remain; ++i)
students.pop_back();
// sort
std::sort(students.begin(), students.end());
}
int main()
{
vector<Student> students;
for (string line; std::getline(cin, line);)
{
istringstream is(line);
string name;
int chinese, math, english;
is >> name >> chinese >> math >> english;
students.emplace_back(name, chinese, math, english);
}
// io done
FilterPassed(students);
cout << "[First round name list]" << endl;
for (auto &e : students)
cout << e << endl;
cout << endl;
cout << "[Final name list]" << endl;
for (int cnt = 3, i = 1; cnt > 0 && i < (int)students.size(); ++i)
{
cout << students[i-1] << endl;
if (students[i] != students[i-1]) --cnt;
}
return 0;
}