因此,我正在努力学习如何为我需要创建的工具打包.所以我使用了来自Bin Packing Problem (Minimize number of used Bins)的以下代码
int[] weight = { 345,345,175,175,175,175,470,470,470,470,345,345,175,175,200,200,200,200,300,300,400,400,350 };
int c = 3000;
int n = weight.Length;
Console.Write("Total: " + weight.Sum() + "mm in " + GFG.bestFit(weight, n, c) + " pieces max");
和工作程序:
public static int bestFit(int[] weight, int n, int c)
{
// Initialize result (Count of bins)
int res = 0;
// Create an array to store
// remaining space in bins
// there can be at most n bins
int[] bin_rem = new int[n];
// Place items one by one
for (int i = 0; i < n; i++) {
// Find the best bin that
// can accommodate
// weight[i]
int j;
// Initialize minimum space
// left and index
// of best bin
int min = c + 1, bi = 0;
for (j = 0; j < res; j++) {
if (bin_rem[j] >= weight[i]
&& bin_rem[j] - weight[i] < min) {
bi = j;
min = bin_rem[j] - weight[i];
}
}
// If no bin could accommodate weight[i],
// create a new bin
if (min == c + 1) {
bin_rem[res] = c - weight[i];
res++;
}
// Assign the item to best bin
else
bin_rem[bi] -= weight[i];
}
return res;
}
所以我有两个问题,但主要只有一个:
我试图将程序修改为List<>
分,但我错误地失败了.我不需要答案AS-IS
,有一个 idea 或方法就可以了,因为我不是在找人为我工作,只是帮助我理解这件事的诀窍在哪里.任何帮助都将被珍视,即使是一条 comments .
可选问题:我不认为是最好的方法(如果我理解得很好的话),因为我没有try 所有的组合和可能性.有没有什么可以改进这个代码的指南?