思路
根据题意,首先创建对应结构体,分别定义四个vector,根据提交逐个push, 重点是sort
比较函数的逻辑,然后对每个vector排序输出
cpp
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int n, l, h, suc = 0;
typedef struct stu {
int id;
int quality;
int knows;
}stu;
bool cmp(stu a, stu b) {
bool result;
int all_a = a.quality + a.knows;
int all_b = b.quality + b.knows;
if (all_a == all_b) {
if (a.quality == b.quality) {
if (a.id < b.id) {
result = true;
} else {
result = false;
}
} else {
if (a.quality > b.quality) {
result = true;
} else {
result = false;
}
}
} else {
if (all_a > all_b) {
result = true;
} else {
result = false;
}
}
return result;
}
int main() {
scanf("%d %d %d", &n, &l, &h);
vector<stu> first;
vector<stu> second;
vector<stu> third;
vector<stu> fourth;
stu s;
int tid, def, abc;
for (int i = 0; i < n; i ++) {
scanf("%d %d %d", &tid, &def, &abc);
s.id = tid;
s.quality = def;
s.knows = abc;
if (s.quality >= l && s.knows >= l) {
suc ++;
if (s.quality >= h && s.knows >= h) {
first.push_back(s);
continue;
} else if (s.quality >= h && s.knows < h) {
second.push_back(s);
continue;
} else if (s.quality >= s.knows) {
third.push_back(s);
continue;
} else {
fourth.push_back(s);
continue;
}
}
}
sort(first.begin(), first.end(), cmp);
sort(second.begin(), second.end(), cmp);
sort(third.begin(), third.end(), cmp);
sort(fourth.begin(), fourth.end(), cmp);
printf("%d\n", suc);
for (int i = 0; i < first.size(); i ++) {
printf("%d %d %d\n", first[i].id, first[i].quality, first[i].knows);
}
for (int i = 0; i < second.size(); i ++) {
printf("%d %d %d\n", second[i].id, second[i].quality, second[i].knows);
}
for (int i = 0; i < third.size(); i ++) {
printf("%d %d %d\n", third[i].id, third[i].quality, third[i].knows);
}
for (int i = 0; i < fourth.size(); i ++) {
printf("%d %d %d", fourth[i].id, fourth[i].quality, fourth[i].knows);
if (i != fourth.size() - 1) {
printf("\n");
}
}
return 0;
}
原文地址:https://www.cnblogs.com/esrevinud/p/11954961.html
时间: 2024-10-29 19:07:51