提交时间:2024-07-03 15:31:11

运行 ID: 49325

#include <iostream> #include <vector> #include <algorithm> #include <iomanip> // 定义一个结构体来存储每个人的编号和接水时间 struct Person { int id; int waterTime; }; // 比较函数,用于排序 bool compare(Person &a, Person &b) { return a.waterTime < b.waterTime; } int main() { int n; std::cin >> n; std::vector<Person> people(n); // 读取接水时间,并给每个人一个编号 for (int i = 0; i < n; ++i) { std::cin >> people[i].waterTime; people[i].id = i + 1; } // 按接水时间排序 std::sort(people.begin(), people.end(), compare); // 计算平均等待时间 double totalWaitTime = 0; double currentWaitTime = 0; for (int i = 0; i < n; ++i) { totalWaitTime += currentWaitTime; currentWaitTime += people[i].waterTime; } // 输出排队顺序 for (const auto &person : people) { std::cout << person.id << " "; } std::cout << std::endl; // 输出平均等待时间,精确到小数点后两位 std::cout << std::fixed << std::setprecision(2) << totalWaitTime / n << std::endl; return 0; }