day4//vector中sort()的使用

本文介绍了一种方法,如何使用C++将整数向量的每个元素平方后,通过sort函数进行从小到大的排序。通过代码实例展示了Solution类中的sortedSquares函数,探讨了算法效率和优化技巧。

 

sort(nums.begin(),nums.end())从小到大排序

class Solution {

public:

vector<int> sortedSquares(vector<int>& nums) {

int size = nums.size();

int j=0;

for(int i =0;i<size;i++)

{

nums[j]=nums[i]*nums[i];

j++;

}

sort(nums.begin(),nums.end());

return nums;

}

};

#include <iostream> #include <queue> //用优先队列,按过期日期管理牛奶批次 #include <vector> #include <string> using namespace std; //存储每批牛奶的信息,过期日期和数量 struct Milk_information { int expireDay; // 牛奶过期日期 int quantity; // 该批次牛奶的升数量 //确保过期日期早的牛奶排在队首,优先被消耗 bool operator<(const Milk_information& other) const { return expireDay > other.expireDay; } }; //管理单个品牌牛奶类的处理采购、消耗、过期处理 class MilkManager { private: //优先队列存储牛奶批次,队首是最早过期的批次 priority_queue<Milk_information> batches; int totalCost; //累计消耗量 int totalWaste; //累计处理量 public: MilkManager() : totalCost(0), totalWaste(0) {} //构造函数:初始化累计量为0 //采购牛奶,计算过期日,加入队列 void purchase(int purchaseDay, int shelfLife, int quantity) { //采购日、保质期天数、采购数量 int expireDay = purchaseDay + shelfLife; //过期日=采购日+保质期 batches.push({ expireDay, quantity }); //加入队列 } // 处理过期牛奶,移除已过期的,累计处理量 void handleExpired(int currentDay) { //当前日期 //循环检查队首批次:若过期日<当前日,说明已过期 while (!batches.empty() && batches.top().expireDay < currentDay) { totalWaste += batches.top().quantity; //累计处理量 batches.pop(); //移除过期牛奶 } } // 消耗牛奶,从最早过期的批次开始消耗,返回消耗量 int cost(int currentDay, int quantity) { //当前日期、需要消耗的数量 handleExpired(currentDay); // 先处理已过期的牛奶 int actualCost = 0; //实际消耗的数量 int remaining = quantity; //剩余需要消耗的数量 vector<Milk_information> temp; //使用temp临时存储未消耗完的批次 //循环,直到满足需求或无可用牛奶 while (remaining > 0 && !batches.empty()) { Milk_information current = batches.top(); //取最早过期的批次 batches.pop(); //弹出队列 if (current.quantity <= remaining) { //当前批次数量不足,全部消耗 remaining -= current.quantity; actualCost += current.quantity; } else { //当前批次数量充足,消耗部分后剩余放回 current.quantity -= remaining; actualCost += remaining; remaining = 0; //需求已满足 temp.push_back(current); //剩余部分暂存 } } //将未消耗的批次放回优先队列 //先把队列中剩余的批次移到临时向量temp while (!batches.empty()) { temp.push_back(batches.top()); batches.pop(); } //再把临时向量temp中的批次重新加入队列 for (const auto& batch : temp) { batches.push(batch); } totalCost += actualCost; // 累计总消耗量 return actualCost; } // 获取当前库存量,计算所有未过期批次的总量 int getStock(int currentDay) { //当前日期 handleExpired(currentDay); //处理过期品 int stock = 0; //总库存 vector<Milk_information> temp; // 临时存储批次 // 遍历所有批次,累加数量 while (!batches.empty()) { temp.push_back(batches.top()); stock += batches.top().quantity; batches.pop(); // 弹出队列 } // 把批次放回队列 for (const auto& batch : temp) { batches.push(batch); } return stock; } //用于比较A、B品牌过期 int getEarliestExpireDay() const { if (batches.empty()) return -1; //无库存时返回-1 return batches.top().expireDay; } // 获取累计消耗量 int getTotalCost() const { return totalCost; } // 获取累计处理量 int getTotalWaste() const { return totalWaste; } }; int main() { int n; // 操作记录条数 cin >> n; // 初始化两个品牌的牛奶管理器:A保质期7天,B保质期15天 MilkManager brandA; MilkManager brandB; int currentDay; //操作发生日期 string operation; // 操作类型,如purchase、cost、check for (int i = 0; i < n; i++) { cin >> currentDay >> operation; if (operation == "purchase") { //采购操作,输入A、B品牌的采购量 int aQuantity, bQuantity; cin >> aQuantity >> bQuantity; //采购A品牌,保质期7天 brandA.purchase(currentDay, 7, aQuantity); //采购B品牌,保质期15天 brandB.purchase(currentDay, 15, bQuantity); } else if (operation == "cost") { //消耗操作 int quantity; cin >> quantity;//:输入需要消耗的总量 //先处理两个品牌的过期牛奶 brandA.handleExpired(currentDay); brandB.handleExpired(currentDay); int remaining = quantity; // 剩余需要消耗的量 // 循环消耗,直到满足需求 while (remaining > 0) { // 获取当前A、B的可用库存 int aStock = brandA.getStock(currentDay); int bStock = brandB.getStock(currentDay); if (aStock == 0) { // 只有B有库存,消耗B remaining -= brandB.cost(currentDay, remaining); } else if (bStock == 0) { // 只有A有库存,消耗A remaining -= brandA.cost(currentDay, remaining); } else { //如果两者都有库存,比较最早过期日,优先消耗更早过期的 int earliestA = brandA.getEarliestExpireDay(); int earliestB = brandB.getEarliestExpireDay(); if (earliestA <= earliestB) { //A更早过期,消耗A remaining -= brandA.cost(currentDay, remaining); } else { //B更早过期,消耗B remaining -= brandB.cost(currentDay, remaining); } } } } else if (operation == "check") { //计算当前库存并输出 int aStock = brandA.getStock(currentDay); int bStock = brandB.getStock(currentDay); // 按格式输出 cout << "A stock:" << aStock << " cost:" << brandA.getTotalCost() << " waste:" << brandA.getTotalWaste() << endl; cout << "B stock:" << bStock << " cost:" << brandB.getTotalCost() << " waste:" << brandB.getTotalWaste() << endl; } } return 0; }#include <iostream> #include <vector> #include <string> using namespace std; // 定义牛奶结构体,存储每批牛奶的购买日期和数量 struct milk { int buy_day; // 购买日期 int quantity; // 数量 }; // 对A类牛奶库存按过期日期升序排序 void sortA(vector<milk>& stock) { int n = stock.size(); // 冒泡排序 for (int i = 0; i < n - 1; ++i) { for (int j = 0; j < n - 1 - i; ++j) { int exp1 = stock[j].buy_day + 7; // 第j批A类牛奶的过期日期 int exp2 = stock[j + 1].buy_day + 7; // 第j+1批A类牛奶的过期日期 if (exp1 > exp2) { // 若前者过期日期更晚,则交换位置 milk temp = stock[j]; stock[j] = stock[j + 1]; stock[j + 1] = temp; } } } } // 对B类牛奶库存按过期日期升序排序 void sortB(vector<milk>& stock) { int n = stock.size(); // 冒泡排序 for (int i = 0; i < n - 1; ++i) { for (int j = 0; j < n - 1 - i; ++j) { int exp1 = stock[j].buy_day + 15; // 第j批B类牛奶的过期日期 int exp2 = stock[j + 1].buy_day + 15; // 第j+1批B类牛奶的过期日期 if (exp1 > exp2) { // 若前者过期日期更晚,则交换位置 milk temp = stock[j]; stock[j] = stock[j + 1]; stock[j + 1] = temp; } } } } // 移除vector中的第一个元素(模拟栈的使用) void removeFirst(vector<milk>& vec) { if (vec.empty()) return; // 若容器为空,直接返回 // 将所有元素向前移动一位 for (int i = 0; i < vec.size() - 1; ++i) { vec[i] = vec[i + 1]; } vec.pop_back(); // 移除最后一个重复的元素 } int main() { int n; // 操作的总次数 cin >> n; vector<milk> A_stock, B_stock; // 存储A类和B类牛奶的库存 int A_cost = 0, A_waste = 0; // A类牛奶的消耗总量、浪费总量 int B_cost = 0, B_waste = 0; // B类牛奶的消耗总量、浪费总量 // 循环处理n次操作 for (int i = 0; i < n; ++i) { int day; // 操作发生的日期 string move; // 操作类型 cin >> day >> move; // 清理A类牛奶库存中已过期的批次 vector<milk> new_A; // 存储未过期的A类牛奶 for (auto& batch : A_stock) { // 若当前日期 <= 过期日期,则未过期 if (day <= batch.buy_day + 7) { new_A.push_back(batch); } else { // 已过期,计入浪费量 A_waste += batch.quantity; } } A_stock = new_A; // 更新A类库存为未过期批次 // 2. 清理B类牛奶库存中已过期的批次 vector<milk> new_B; // 存储未过期的B类牛奶 for (auto& batch : B_stock) { // 若当前日期 <= 过期日期,则未过期 if (day <= batch.buy_day + 15) { new_B.push_back(batch); } else { // 已过期,计入浪费量 B_waste += batch.quantity; } } B_stock = new_B; // 更新B类库存为未过期批次 // 处理"purchase"操作 if (move == "purchase") { int a_qty, b_qty; // 购买的A类、B类牛奶数量 cin >> a_qty >> b_qty; // 若购买A类牛奶数量>0,添加到A类库存并按过期日期排序 if (a_qty > 0) { A_stock.push_back({ day, a_qty }); // 记录购买日期为当前日期 sortA(A_stock); // 确保A类库存按过期日期升序排列 } // 若购买B类牛奶数量>0,添加到B类库存并按过期日期排序 if (b_qty > 0) { B_stock.push_back({ day, b_qty }); // 记录购买日期为当前日期 sortB(B_stock); // 确保B类库存按过期日期升序排列 } } // 处理"cost"操作(消耗牛奶) else if (move == "cost") { int need; // 需要消耗的牛奶总量 cin >> need; // 循环消耗,直到满足需求 while (need > 0) { bool use_A = false; // 标记是否使用A类牛奶 // 判断是否使用A类牛奶 if (!A_stock.empty()) { if (B_stock.empty()) { use_A = true; // B类为空,只能用A类 } else { int a_exp = A_stock[0].buy_day + 7; int b_exp = B_stock[0].buy_day + 15; use_A = (a_exp < b_exp); } } // 使用A类牛奶消耗 if (use_A) { milk& batch = A_stock[0]; // 取A类最早过期的批次 if (batch.quantity >= need) { batch.quantity -= need; A_cost += need; // 累计A类消耗 need = 0; // 需求满足,退出循环 } else { A_cost += batch.quantity; // 累计A类消耗 need -= batch.quantity; // 剩余需求 removeFirst(A_stock); // 移除已消耗完的批次 } } // 使用B类牛奶消耗,同A操作 else { milk& batch = B_stock[0]; if (batch.quantity >= need) { batch.quantity -= need; B_cost += need; need = 0; } else { B_cost += batch.quantity; need -= batch.quantity; removeFirst(B_stock); } } } } // 处理"check"操作 else if (move == "check") { // 计算A类当前库存总量 int A_stock_qty = 0; for (auto& batch : A_stock) A_stock_qty += batch.quantity; // 计算B类当前库存总量 int B_stock_qty = 0; for (auto& batch : B_stock) B_stock_qty += batch.quantity; // 输出A类和B类的库存、消耗、浪费信息 cout << "A stock:" << A_stock_qty << " cost:" << A_cost << " waste:" << A_waste << endl; cout << "B stock:" << B_stock_qty << " cost:" << B_cost << " waste:" << B_waste << endl; } } return 0; }根据我提供的两个方案写出第三个方案 要求查重率不到80%
最新发布
11-09
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值