【已通过】 注意:排序必须用稳定的排序方法,sort快速排序不能用!!! 代码如下:
#include<string>
#include<algorithm>
using namespace std;
#define MAXNUM 1024
struct Student {
public:
string name;
int score;
}s[MAXNUM];
void mysort(int n, bool b) {
if (b) {
for (int i = n - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (s[j].score > s[j+1].score) {
Student temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
}
}
else {
for (int i = n - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (s[j].score < s[j+1].score) {
Student temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
}
}
}
void test() {
int arr[] = { 3,5,1,10,2 };
sort(arr, arr + 5);
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
//test();
int n;
while (cin >> n) {
bool b; cin >> b;//0(降序)或者1(升序)
for (int i = 0; i < n; i++) {
cin >> s[i].name >> s[i].score;
}
mysort(n, b);
for (int i = 0; i < n; i++) {
cout << s[i].name << " " << s[i].score << endl;
}
}
return 0;
}