//仓库类,对应每个仓库
public class store
{
private int _max;//数量
public int max { get { return _max; } set { _max = value; } }
private string _name;//名称
public string name { get { return _name; } set { _name = value; } }
public int count { get { return _max; } set { _max = value; } } //剩余库位
public HashSet<int> wares;//已用库位
public bool push(material m) { return true; } //入库 检查wares属性,如库位已被占用,就跳到后面的库位,如仓库已满返回false
public bool pop(material m) { return true; } //出库 从wares属性移除m public store(int max,string name)
{
this.max = max;
this.name = name;
}
} //物料类,单个物料
public class material { } //操作数据
public class mtradapter
{
public int s_in(material[] mtrs)
{
store[] s = { new store(800, "a"), new store(600, "b"), new store(500, "c"), new store(400, "d") };
int cur = 0; //迭代变量
int last = mtrs.Length; //用于记录未入库数量
for (int i = 0; i < mtrs.Length; i++)
{
if (s[cur].count <= 0) cur++; //当前仓库已满,换后面仓库
if (cur >= s.Length)
{
//仓库已用完
last = mtrs.Length - i; //还有多少没入库
break;
}
if (s[cur].push(mtrs[i])) //入库,按理说这里不需要再判断,但是为了代码更健壮,应该让store类自己判断结果
{
//入库成功
}
else
{
//入库失败
}
}
return last;
}
}
var StoreQuantity = new List<int> { 0, 0, 0, 0 }; //实际库存量
St(900, StoreCapacity, StoreQuantity);
St(800, StoreCapacity, StoreQuantity);
然后实现所谓的算法:public static void St(int quantity, List<int> StoreCapacity, List<int> StoreQuantity)
{
for (var i = 0; i < StoreQuantity.Count; i++)
{
if (StoreQuantity[i] < StoreCapacity[i])
{
var q = Math.Min(quantity, StoreCapacity[i] - StoreQuantity[i]);
StoreQuantity[i] += q;
quantity -= q;
if (quantity == 0)
return;
}
}
}