里面有测试成绩为0的情况。然后我的最小成绩设成0了,然后0的排序有问题。改成-1就好了。
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
struct Node {
string id;
int grades;
int local;
int local_rank;
int fina_ranl;
};
vector <Node> list;
int local, N;
bool cmp(Node n1, Node n2) {
if (n1.grades > n2.grades)
return true;
else if (n1.grades == n2.grades && n1.id < n2.id) {
return true;
}
return false;
}
bool cmpLocal(Node n1, Node n2) {
return n1.local < n2.local;
}
int main() {
cin >> local;
Node temp;
vector <int> localnum ;
int rank , tempGrande ;
for (int i = 0; i < local; i++) {
cin >> N;
localnum.push_back(N);
int st = list.size();
for (int j = 0; j < N; j++) {
cin >> temp.id >> temp.grades;
temp.local = i+1;
list.push_back(temp);
}
int ed = list.size();
tempGrande = -1;
// cout << st << " ed " << ed << endl;
sort(&list[st], &list[st]+ N, cmp);
for (int j = st, r = 1; j < ed; j++) {
if (tempGrande != list[j].grades) {
r = j - st + 1;
tempGrande = list[j].grades;
}
list[j].local_rank = r;
}
}
#ifdef _DEBUG
cout << endl;
for (int i = 0; i < list.size(); i++) {
cout << list[i].id << " " << list[i].grades << " " << list[i].fina_ranl << " "
<< list[i].local << " " << list[i].local_rank << endl;
}
cout << endl;
#endif
int len = list.size();
//最终排序
sort(&list[0], &list[0] + len, cmp);
tempGrande = -1;
for (int i = 0,r = 0; i < len; i++) {
if (tempGrande != list[i].grades) {
r = i+1; tempGrande = list[i].grades;
}
list[i].fina_ranl = r;
}
cout << list.size() << endl;
for (int i = 0; i < list.size(); i++) {
cout << list[i].id << " " << list[i].fina_ranl << " "
<< list[i].local << " " << list[i].local_rank << endl;
}
system("pause");
return 0;
}