我正在嘗試解決填充串列中的每個專案直到每個專案都達到上限的概念。一旦每個專案都達到上限/限制,任何剩余的超額值都會從串列的第一項開始再次分配。電流擊穿后的例子。
我相信我對問題的第一個組成部分有解決方案,即分配價值 - 但是我不確定處理超額價值的解決方案。
一個詳細的例子:
我有一個盒子串列(
10盒子)和一個33產品價值,我想在這些盒子中一次分發一個產品,直到設定每個盒子的限制上限(5產品)。5在我再次選擇我的第一個盒子并一次開始一個盒子以添加剩余的多余部分之前,必須填寫每個盒子。
在下表中,init是存盤在每個框中的預先存在的值。接下來的列顯示了 、 和 finally 的值在方框中25的33分布40。在40您可以更清楚地看到填充最空盒子的優先級如何最終填充所有其他盒子。
| 在里面 | 25 | 33 | 40 |
|---|---|---|---|
| 2 | 4 | 5 | 6 |
| 1 | 3 | 5 | 6 |
| 2 | 4 | 5 | 5 |
| 0 | 2 | 4 | 5 |
| -1 | 1 | 3 | 5 |
| 2 | 4 | 5 | 5 |
| 3 | 5 | 5 | 5 |
| 0 | 2 | 4 | 5 |
| 2 | 4 | 5 | 5 |
| 1 | 3 | 4 | 5 |
我現在的邏輯是這樣的:
class Box
{
public float amount;
}
public static void Init()
{
// Create a list of boxes.
List<Box> boxes = new List<Box>
{
// Add boxes to the list.
new Box() { amount = 2 },
new Box() { amount = 1 },
new Box() { amount = 2 },
new Box() { amount = 0 },
new Box() { amount = -1 },
new Box() { amount = 2 },
new Box() { amount = 3 },
new Box() { amount = 0 },
new Box() { amount = 2 },
new Box() { amount = 1 }
};
Distribute(boxes, 25, 2);
}
static void Distribute(List<Box> boxes, float totalValue, float maxDistribution)
{
float ceiling = 5; //The "soft limit" for each box.
List<Box> currentBoxes = boxes;
List<Box> criticalBoxes = new List<Box>();
int distribution = (int)(totalValue / maxDistribution); //amount of times needed for even distribution.
int currentBoxIndex = 0;
for (int i = 0; i < distribution; i )
{
//If we have room, add distribution, else move to a "at capacity list"
if (currentBoxes[currentBoxIndex].amount maxDistribution < ceiling)
{
currentBoxes[currentBoxIndex].amount = maxDistribution; //Distribute the maximum allowed per loop
} else
{
criticalBoxes.Add(currentBoxes[currentBoxIndex]);
currentBoxes.RemoveAt(currentBoxIndex);
}
//Status of loop.
if(currentBoxIndex == currentBoxes.Count)
{
currentBoxIndex = 0;
} else
{
currentBoxIndex ;
}
}
}
uj5u.com熱心網友回復:
它似乎是二進制的;您必須分配的數量將完全填滿盒子(有/沒有溢位),或者不會。如果會,則從它們全部開始并分配溢位物,否則,分配到上限
int distrib = 40;
int cap = 5;
int space = boxes.Sum(b => cap - b.Amount);
int overspill = distrib - space;
if(overspill >= 0){
boxes.ForEach(b => b.Amount = cap overspill / boxes.Count);
distrib = overspill % boxes.Count;
cap = int.MaxValue;
}
for(int x= 0; x < distrib;){
var b = boxes[x%boxes.Count];
if(b.Amount >= cap)
continue;
boxes[x%boxes.Count].Amount ;
x ;
}
相反,如果您想要對最空的盒子進行優先排序的邏輯,則使用 MinBy(.net6 或 MoreLinq)會變得更簡單
int distrib = 25;
while(distrib-- > 0)
boxes.MinBy(b => b.Amount).Amount ;
在較舊的.net中boxes.First(b => b.Amount == boxes.Min(b2 => b2.Amount) ).Amount ;
有很多優化和調整的空間,但是值得付出多少努力取決于它有多少熱點
uj5u.com熱心網友回復:
我認為這很接近:
List<Box> boxes = new List<Box>
{
// Add boxes to the list.
new Box() { amount = 2 },
new Box() { amount = 1 },
new Box() { amount = 2 },
new Box() { amount = 0 },
new Box() { amount = -1 },
new Box() { amount = 2 },
new Box() { amount = 3 },
new Box() { amount = 0 },
new Box() { amount = 2 },
new Box() { amount = 1 }
};
Console.WriteLine(String.Join(", ", boxes.Select(b => b.amount)));
var sorted = boxes.OrderBy(b => b.amount < 5f ? int.MinValue : b.amount).ToList();
while (boxes.Sum(b => b.amount) < 52f)
{
var box = sorted.First();
sorted.RemoveAt(0);
box.amount = 1.0f;
sorted = sorted.Append(box).OrderBy(b => b.amount < 5f ? int.MinValue : b.amount).ToList();
Console.WriteLine(String.Join(", ", boxes.Select(b => b.amount)));
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