【问题描述】某银行年终举行答谢会,现读入若干客户信息,以"end"作为结束。请按客户余额从大到小给出邀请名单,客户余额相同时按开户日期先后排序,开户日期相同则按照客户姓名进行升序排列。
【输入形式】输入包括若干行,每行包括客户名、客户余额和开户日期(年月日),以空格作为分隔,最后一行为"end"
【输出形式】输出包括若干行,每行只包含客户名称.
【样例输入】
Alice 5000.0 20220101 Bob 8000.0 20211215 Amy 7000.0 20220205 Charlie 6000.0 20220120 Tom 7000.0 20211130 end
【样例输出】
Bob Tom Amy Charlie Alice
【样例说明】
根据余额降序,则Bob排在第一位,Tom和Amy余额相同,则开户日期更前的Tom排在前面。
【数据规模】
客户人数大于等于1,小于等于100,客户余额可能包含小数,50%的测试用例数据没有重复,且保证每个测试中的客户姓名互不相同客户名中不包含空格符。
【提示】
正确设置开户日期的数据类型,方便直接比较。
答案:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
// 定义结构体存储客户信息
struct Customer {
std::string name;
double balance;
int openDate;
};
// 比较函数,用于自定义排序规则
bool compare(const Customer &a, const Customer &b) {
if (a.balance > b.balance) {
return true;
} else if (a.balance == b.balance) {
if (a.openDate < b.openDate) {
return true;
} else if (a.openDate == b.openDate) {
return a.name < b.name;
}
}
return false;
}
int main() {
std::vector<Customer> customers;
std::string name;
double balance;
std::string dateStr;
int openDate;
while (true) {
std::cin >> name;
if (name == "end") {
break;
}
std::cin >> balance;
std::cin >> dateStr;
openDate = std::stoi(dateStr);
Customer c = {name, balance, openDate};
customers.push_back(c);
}
// 对客户信息进行排序
std::sort(customers.begin(), customers.end(), compare);
for (const auto &customer : customers) {
std::cout << customer.name << std::endl;
}
return 0;
}