2023-04-02:设计一个仓库管理器,提供如下的方法: 1) void supply(String item, int num, int price) 名字叫item的商品,个数num,价格pri
2023-04-02:設計一個倉庫管理器,提供如下的方法:
- void supply(String item, int num, int price)
名字叫item的商品,個數num,價格price。 - int sell(String item, int num)
賣出叫item的商品,個數num個,價格從低到高,返回賣出總價。
如果商品很多,每種商品的數量可能很多,該怎么設計這個結構。
答案2023-04-02:
設計思路
我們需要設計一個倉庫管理器,它可以支持以下幾種操作:
1.向指定商品中進貨一定數量的貨物;
2.對指定商品進行售賣,并返回售賣的總價。
為了實現這些功能,我們需要對每種商品維護一個最大堆,堆中存儲的是該商品的所有不同價格,按照從低到高的順序排列。同時,我們還需要一個哈希表,記錄每個價格對應的商品數量。
在進貨時,我們首先根據傳入的商品名稱,在哈希表中查找是否已經有該商品信息。如果有,則直接將新貨物數量加入相應的價格中;否則,我們就需要創建一個新的最大堆和哈希表項,并將新貨物信息添加到其中。
在售賣時,我們需要按照從低到高的價格順序逐個處理商品。具體來說,我們從最大堆中彈出最低價格的商品,然后查詢其數量是否足夠售賣。如果數量足夠,那么我們將相應的金額累加到總價中,并從哈希表中刪除對應的價格項;否則,我們只能將部分商品出售,并將剩余商品放回最大堆中,等待下一次處理。
主要數據結構
為了實現上述功能,我們需要使用以下幾種數據結構:
1.哈希表(HashMap):用于記錄每個商品的價格和數量信息;
2.最大堆(BinaryHeap):存儲每個商品的所有不同價格,并按照從低到高的順序排列。
具體實現
在 Rust 中,我們可以通過定義一個名為 Store 的結構體來表示每種商品的價格數量信息。該結構體包含兩個字段:
1.price_nums:本質上是一個哈希表,記錄了每個價格對應的商品數量;
2.heap:是一個最大堆,存儲了所有不同價格的商品,便于在售賣時按照價格從低到高進行處理。
struct Store {
price_nums: HashMap<i32, i32>,
heap: BinaryHeap<i32>,
}
impl Store {
fn new() -> Self {
Self {
price_nums: HashMap::new(),
heap: BinaryHeap::new(),
}
}
fn add(&mut self, num: i32, price: i32) {
if let Some(count) = self.price_nums.get_mut(&price) {
*count += num;
} else {
self.price_nums.insert(price, num);
self.heap.push(price);
}
}
fn remove(&mut self, mut num: i32) -> i32 {
let mut money = 0;
while !self.heap.is_empty() && num != 0 {
let price = self.heap.pop().unwrap();
let stores = *self.price_nums.get(&price).unwrap();
if num >= stores {
money += price * stores;
self.price_nums.remove(&price);
num -= stores;
} else {
money += price * num;
self.heap.push(price);
self.price_nums.insert(price, stores - num);
break;
}
}
money
}
}
接下來,我們可以定義一個名為 StoreManager 的結構體,用于管理所有商品的進貨和售出操作。該結構體包含一個哈希表 map,鍵是商品名稱,值是對應的 Store 對象。
pub struct StoreManager {
map: HashMap<String, Store>,
}
在 supply 方法中,我們根據傳入的商品名稱在哈希表中查找是否已經有該商品信息。如果有,則直接將新貨物數量加入相應的價格中;否則,我們就需要創建一個新的最大堆和哈希表項,并將新貨物信息添加到其中。
在 sell 方法中,我們首先通過商品名稱找到對應的 Store 對象,然后調用其 remove 方法進行售賣操作。在這個方法里,我們首先從最大堆中彈出價格最低的商品,然后查看其數量是否足夠售賣。如果該商品數量大于等于需要售賣的數量,那么直接將總價增加相應的金額,并刪除該商品;否則將總價增加當前售賣所需的金額,并將剩余的商品放回最大堆中。
impl StoreManager {
pub fn supply(&mut self, item: &str, num: i32, price: i32) {
let store = self.map.entry(item.to_string()).or_insert(Store::new());
store.add(num, price);
}
pub fn sell(&mut self, item: &str, num: i32) -> i32 {
self.map.get_mut(item).unwrap().remove(num)
}
}
rust完整代碼
use std::collections::{BinaryHeap, HashMap};
struct Store {
price_nums: HashMap<i32, i32>,
heap: BinaryHeap<i32>,
}
impl Store {
fn new() -> Self {
Self {
price_nums: HashMap::new(),
heap: BinaryHeap::new(),
}
}
fn add(&mut self, num: i32, price: i32) {
if let Some(count) = self.price_nums.get_mut(&price) {
*count += num;
} else {
self.price_nums.insert(price, num);
self.heap.push(price);
}
}
fn remove(&mut self, mut num: i32) -> i32 {
let mut money = 0;
while !self.heap.is_empty() && num != 0 {
let price = self.heap.pop().unwrap();
let stores = *self.price_nums.get(&price).unwrap();
if num >= stores {
money += price * stores;
self.price_nums.remove(&price);
num -= stores;
} else {
money += price * num;
self.heap.push(price);
self.price_nums.insert(price, stores - num);
break;
}
}
money
}
}
pub struct StoreManager {
map: HashMap<String, Store>,
}
impl StoreManager {
pub fn new() -> Self {
Self {
map: HashMap::new(),
}
}
pub fn supply(&mut self, item: &str, num: i32, price: i32) {
let store = self.map.entry(item.to_string()).or_insert(Store::new());
store.add(num, price);
}
pub fn sell(&mut self, item: &str, num: i32) -> i32 {
self.map.get_mut(item).unwrap().remove(num)
}
}
fn main() {
let mut manager = StoreManager::new();
manager.supply("apple", 10, 3);
manager.supply("banana", 5, 2);
manager.supply("orange", 8, 4);
let total_price = manager.sell("banana", 3);
println!("Sell 3 bananas: ${}", total_price);
let total_price = manager.sell("banana", 2);
println!("Sell 2 bananas: ${}", total_price);
let total_price = manager.sell("apple", 5);
println!("Sell 5 apples: ${}", total_price);
let total_price = manager.sell("orange", 10);
println!("Sell 10 oranges: ${}", total_price);
}
總結
以上是生活随笔為你收集整理的2023-04-02:设计一个仓库管理器,提供如下的方法: 1) void supply(String item, int num, int price) 名字叫item的商品,个数num,价格pri的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HashSet 和 HashMap 的比
- 下一篇: grunt对象之api