2014-11-12 1 views
1
class Employee 
{ 
    int id; 
    Position position; 
} 

class Position 
{ 
    string name; 
} 

public List<List<Tuple<Position, Employee>>> getAllCombinations(Dictionary<Position, int> positionToCountMap, List<Employee> allEmployees) 
{ 
} 

positionToCountMap을 { "Manager", 1}, { "Developer", "3"}, { "PM ","1 "} (얼마나 많은 키가 있는지 알 수 없음)여러 목록의 모든 조합을 가져와 각 목록에서 금액을 가져와야합니다.

가능한 모든 조합의 allEmployees 목록을 반환해야 positionToCountMap의 계산 요구 사항을 충족시킵니다. 1 명의 관리자, 3 명의 개발자 및 1 명의 PM이 모두 필요합니다. 나의 첫 걸음은 그 위치에있는 직원 목록에 새로운 위치 사전을 만드는 것이 었습니다.

var positionToEmployeeMap = new Dictionary<Position, List<Employee>>() 
//Loop through allEmployees adding each to this dictionary 

이제 여러 가지 목록이 생겨서 각 목록에서 positionToCountMap에 지정된 양을 취하는 모든 가능한 조합을 찾아야합니다.

이 방법으로 문제를 해결할 수 있습니까? 그것이 사실이라 할지라도, 나는 이것이 실제로 무차별 적으로 강요 할 것이라는 말로 머리를 감쌀 수 없다. 원래 재귀 적 솔루션을 생각하려고했지만 목록의 크기가 커져서 재귀가 큰 선택이 아닐 수도 있습니다. 나는 붙어있어 조언을 사용할 수 있습니다.

가 잘되지 그리고 난 여전히 몇 가지 조언을 얻을 싶어요하지만 내가, 내가 해결책을 가지고 생각 편집 할 수 있습니다.

var positionToEmployeeMap = new Dictionary<Position, List<Employee>>() 
//Loop through allEmployees adding each to this dictionary 

var relevantLists = new List<Employee>(); 
//for each key in positionToCountMap, find the list in positionToEmployeeMap and add it to relevantLists 

var allCombos = new List<List<Employee>>(); 
//Loop through relevantLists. For each list, recursively generate all possible combinations of sublists of size N, where N is the number in positionToCountMap. Add a list of all the combinations to allCombos 

//recursively loop through allCombos finding all possible combinations taking 1 element from each list 

답변

-1

저는 LINQ를 사용합니다. 다음으로 SQL CROSS JOIN 동작으로 비슷한 작업을 수행 할 수 있습니다이 중고 장비 구매, ListB, 그리고 여기서 listc의 값의 모든 조합을 포함하여 목록에 발생합니다

var ListA = new List<object>(); 
var ListB = new List<object>(); 
var ListC = new List<object>(); 

var result = (from a in listA 
    from b in listB 
    from c in listC 
    select new { a, b, c }).ToList(); 

.

+0

문제는 각 목록에서 하나 이상이되어야한다는 것입니다. OP의 위의 예와 같이 목록 A에서 한 항목, 목록 B에서 3 개, 목록 C에서 한 항목이 필요합니다. – user3715648

+0

또한 사전에 얼마나 많은 키가 있는지 알 수 없으므로 오직 3 목록입니다. – user3715648

관련 문제