큰 개체를 먼저 저장하고 작게 만들려고하면 쉽습니다. 여기
내가 당신을 위해 만들어 빠른 코드, 그리고 그것을 잘 작동합니다 : 예를 들어
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
편집 :
// 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()));
: 당신이 180에서 목록만큼 원하는 경우이는 과정과 뷰 사이에 추가 할 수 있습니다 (quicky와 아가씨) 코드
는이 코드에 만족하지 해요,하지만
새로운 결과를 작동하는 것 같다 :
을
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
나는 아이디어를 정확하게 가지고 있는지 잘 모르겠다. 하지만 냅닥 문제에 대해 아십니까? http://en.wikipedia.org/wiki/Knapsack_problem –
@SpaceApple : 임의 순서로 가져올 수 있습니다. – user2190141