————————————————
难在自定义比较函数那吧,要把关系理清楚,其他还好。 #include<iostream> #include<cstring> #include<algorithm> using namespace std; struct kaosheng { char id[15]; int defen; int caifen; int sum; int flag; }kao[100001]; bool cmp(kaosheng a, kaosheng b) { if (a.flag != b.flag) return a.flag < b.flag; else if (a.sum != b.sum) return a.sum > b.sum; else if (a.defen != b.defen) return a.defen > b.defen; else return strcmp(a.id, b.id) < 0; } int main() { int n, low, high; int countjige = 0; cin >>n>> low >> high; for (int i = 0; i < n; i++) { cin >> kao[i].id >> kao[i].defen >> kao[i].caifen; kao[i].sum = kao[i].defen + kao[i].caifen; if (kao[i].caifen >= high && kao[i].defen >= high) { kao[i].flag = 1; countjige++; } else if (kao[i].caifen < high && kao[i].caifen >= low && kao[i].defen >= high) { kao[i].flag = 2; countjige++; } else if (kao[i].caifen < high && kao[i].caifen >= low && kao[i].defen < high && kao[i].defen >= low && kao[i].defen >= kao[i].caifen) { kao[i].flag = 3; countjige++; } else if ( kao[i].caifen >= low && kao[i].defen >= low ) { kao[i].flag = 4; countjige++; } else kao[i].flag = 5; } sort(kao, kao + n, cmp); cout << countjige << endl; for (int i = 0; i <countjige; i++) { cout << kao[i].id << " " << kao[i].defen << " " << kao[i].caifen << endl; } }