-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4_A1025.cpp
More file actions
182 lines (163 loc) · 4.31 KB
/
4_A1025.cpp
File metadata and controls
182 lines (163 loc) · 4.31 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* P103 Score Ranking
*/
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#define MAX 300
using namespace std;
// define structure
/*
typedef struct Student {
// 13 digits, so cannot use 'int'
string registerNumber;
// score
int score;
// location
int locationNumber;
// location rank
int locationRank;
};
*/
class Student {
private:
// 考号
string registerNumber;
// 分数
int score;
// 考场号
int locationNumber;
// 考场内排名
int locationRank;
public:
// constructor
Student(string registerNumber, int score, int locationNumber)
{
this->score = score;
this->registerNumber = registerNumber;
this->locationNumber = locationNumber;
}
// getter and setter
string getRegisterNumber()
{
return this->registerNumber;
}
int getScore()
{
return this->score;
}
int getLocationNumber()
{
return this->locationNumber;
}
int getLocationRank()
{
return this->locationRank;
}
void setRegisterNumber(string registerNumber)
{
this->registerNumber = registerNumber;
}
void setScore(int score)
{
this->score = score;
}
void setLocationNumber(int locationNumber)
{
this->locationNumber = locationNumber;
}
void setLocationRank(int locationRank)
{
this->locationRank = locationRank;
}
};
// sort(first_loaction, the_next_of_last_location, cmp_function)的比较函数
bool cmp(Student a, Student b)
{
// a b分数不同,则按分数从大到小
if (a.getScore() != b.getScore())
{
return a.getScore() > b.getScore();
}
// 分数相同,按照 registerNumber 字典序从小到大
else
{
return (a.getRegisterNumber()< b.getRegisterNumber());
}
}
void rankFunction(int &locationTotal, int &studentTotal, vector<Student> &studentList)
{
cout << "Input location number: ";
cin >> locationTotal;
// 循环按考场添加
for (int i = 1; i <= locationTotal; i++)
{
// 考场内的人数
int k;
cout << "Input testee number of this location: ";
cin >> k;
// 循环添加学生信息
for (int j = 0; j < k; j++)
{
string registerNumber;
int score;
cout << "Input testee's info: ";
cin >> registerNumber >> score;
// set info and push into vector
studentList.push_back(Student(registerNumber, score, i));
studentTotal += 1;
}
// 考场内进行排序
// 第一个是起始元素,第二个是尾元素的下一个地址,最后一个是比较函数
sort(studentList.begin() + studentTotal - k, studentList.begin() + studentTotal, cmp);
// set student's locationRank
for (int m = studentTotal - k; m < studentTotal; m++)
{
// 第一个学生
if (m == studentTotal - k)
{
studentList[m].setLocationRank(1);
}
// 其他学生
else
{
// 如果分数跟前面一个同学的分数相同,则排名一致
if (studentList[m].getScore() == studentList[m - 1].getScore())
{
studentList[m].setLocationRank(studentList[m - 1].getLocationRank());
}
else
{
studentList[m].setLocationRank(m - studentTotal + k + 1);
}
}
}
}
// 对整个vector进行排序
sort(studentList.begin(), studentList.end(), cmp);
}
int main()
{
// 考场的总数
int locationTotal;
// 考生总人数
int studentTotal = 0;
// Student Array
vector<Student> studentList;
// function
rankFunction(locationTotal, studentTotal, studentList);
// rank value
int rank = 1;
// print out
for (int i = 0; i < studentTotal; i++)
{
if (i > 0 && studentList[i - 1].getScore() != studentList[i].getScore())
{
rank = i + 1;
}
cout << studentList[i].getRegisterNumber() << " " << rank << " "
<< studentList[i].getLocationNumber() << " " << studentList[i].getLocationRank() << endl;
}
return 0;
}