```cpp #include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { int n; // 读取书的数量 cin >> n; vector<int> prices(n); for (int i = 0; i < n; i++) { // 读取每本书的价格 cin >> prices[i]; } // 对书的价格进行降序排序 sort(prices.begin(), prices.end(), greater<int>()); int totalCost = 0; for (int i = 0; i < n; i++) { // 如果索引不是3的倍数,说明这本书需要付费 if ((i + 1) % 3 != 0) { totalCost += prices[i]; } } // 输出买下全部书的最少花费 cout << totalCost << endl; return 0; } ``` 我在以往的开发过程中,碰到过以下几点需要格外小心处理的地方: 1. 排序时要注意是降序排序,这样才能保证每次买二送一得到最大的优惠。 2. 计算索引时要注意索引是从0开始的,判断是否为3的倍数时要进行合适的处理。 3. 输入输出的格式要严格按照题目要求,避免因为格式问题导致结果错误。 [2025-08-14 15:09:51 | AI写代码神器 | 315点数解答]