如果您嘗試首先存儲大對象並以較小的值完成,可能很容易。
下面是一個簡單的代碼我爲你而造,並能正常工作:
static void Main(string[] args)
{
// data
List<Int32> listElement = new List<Int32>() { 10, 20, 10, 30, 45, 10, 20, 30, 40, 50, 60, 40, 30, 50, 60, 70, 80, 90, 20, 30, 10, 50, 60, 40, 60, 80, 90, 60, 80, 70, 80, 90, 90, 50 };
Int32 MaxStack = 180;
// result
List<List<Int32>> listResult = new List<List<Int32>>();
// process
foreach (Int32 element in listElement.OrderByDescending(i => i))
{
List<Int32> listToStore = listResult.Where(l => l.Sum() + element <= MaxStack).FirstOrDefault();
if (listToStore == null)
{
listToStore = new List<Int32>();
listResult.Add(listToStore);
}
listToStore.Add(element);
}
// view
foreach (List<Int32> list in listResult)
{
Console.Write("List " + (listResult.IndexOf(list) + 1) + "[total " + list.Sum() + "]: ");
foreach (Int32 element in list)
{
Console.Write(element.ToString() + " ");
}
Console.WriteLine();
}
Console.ReadKey();
}
對於這個例子,它是在控制檯,使用的Int32對象,但它是複雜的對象相同。
所有的事情是從大到小讀取對象列表,並找到可存儲它的第一個商店列表。
結果是:
List 1[total 180]: 90 90
List 2[total 180]: 90 90
List 3[total 180]: 80 80 20
List 4[total 180]: 80 80 20
List 5[total 180]: 70 70 40
List 6[total 180]: 60 60 60
List 7[total 180]: 60 60 50 10
List 8[total 180]: 50 50 50 30
List 9[total 175]: 45 40 40 30 20
List 10[total 90]: 30 30 10 10 10
編輯:如果你想盡可能多的在180名單,這是你的過程和視圖之間添加一個(quicky和小喬)代碼:
// switching element for better fill
List<List<Int32>> unfilledlist = listResult.Where(l => l.Sum() < MaxStack).ToList();
// truncate original result
unfilledlist.ForEach(l => listResult.Remove(l));
while (unfilledlist != null && unfilledlist.Count > 1)
{
List<Int32> list = unfilledlist.First();
unfilledlist.Remove(list);
foreach (Int32 element in list)
{
Int32 needed = MaxStack - list.Sum() + element;
Boolean isFound = false;
foreach (List<Int32> smallerlist in unfilledlist)
{
List<Int32> switchingList = new List<int>();
// searching how to fill what we needed
foreach (Int32 e in smallerlist.OrderByDescending(i => i))
{
if (e + switchingList.Sum() <= needed)
switchingList.Add(e);
}
// we found a possible switch
if (switchingList.Sum() == needed)
{
// moving first element
list.Remove(element);
smallerlist.Add(element);
// moving element
switchingList.ForEach(e => { smallerlist.Remove(e); list.Add(e); });
isFound = true;
break;
}
}
if (isFound)
break;
}
listResult.Add(list.OrderByDescending(i => i).ToList());
}
// completing result with lists that are not with sum 180
unfilledlist.ForEach(l => listResult.Add(l.OrderByDescending(i => i).ToList()));
我不滿意這個代碼,但它似乎工作
新結果:
List 1[total 180]: 90 90
List 2[total 180]: 90 90
List 3[total 180]: 80 80 20
List 4[total 180]: 80 80 20
List 5[total 180]: 70 70 40
List 6[total 180]: 60 60 60
List 7[total 180]: 60 60 50 10
List 8[total 180]: 50 50 50 30
List 9[total 180]: 40 40 30 30 20 10 10
List 10[total 85]: 45 30 10
我認爲列表中的值是隨機排列的? – SpaceApple 2013-03-20 09:29:22
我不確定我是否完全明白了。但你知道knapsak問題嗎? http://en.wikipedia.org/wiki/Knapsack_problem – 2013-03-20 09:29:25
@SpaceApple:你可以採取隨機順序。 – user2190141 2013-03-20 09:32:22