列表组合C# [英] List Combinations c#
本文介绍了列表组合C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个4集。
{A1,A2,A3,...}
{B1,B1,B3,...}
{C1,C2,C3,...}
{D1,D2,D3 ...}
我需要用下面的规则来查找所有可能的组合:
- 1从第一和第四集合项 LI>
- 2项来自第二集合
- 3项从第三
示例组合:
{A1,B1,B2,C1,C2,C3,D1}
{A2,B1,B2,C1,C2,C3,D1}
解决方案
Visual Studio的时间太长加载,所以我这样做是在JavaScript中,因为我可以测试在我的控制台。这将打印出所有的选择。 (此外,它听起来更像是一个什么是算法呢?而不是有什么算法,这种的在C#的?)
函数makeGroup(ID,计数){
VAR的结果= [];
对于(VAR I = 1; I< =计数;我++){
result.push(ID + I);
}
返回结果;
}
功能选择(组数){
如果(计数=== 1){
VAR的结果= [];
对于(VAR I = 0; I< group.length;我++){
result.push([组[I]);
}
返回结果;
}
如果(计数=== 2){
VAR的结果= [];
对于(VAR I = 0; I< group.length;我++){
对于(VAR J = 0; J< group.length; J ++){
如果(我== J和!&安培;
I< j)条{
result.push([组[I],组[J]);
}
}
}
返回结果;
}
如果(计数=== 3){
VAR的结果= [];
对于(VAR I = 0; I< group.length;我++){
对于(VAR J = 0; J< group.length; J ++){
为(变种K = 0; K< group.length; k ++){
如果(我== J和!&安培;我== K&放大器;!&安培;Ĵ== K&放大器;!&安培;
I< J&安培;&安培; J< K){
result.push([组[I],组[J],组[K]);
}
}
}
}
返回结果;
}
}
VAR A组= makeGroup('A',2);
变种B组= makeGroup('B',2);
变种C组= makeGroup(C,3);
变种D组= makeGroup(D,1);
选择(A组,1).forEach(函数(){
选择(B组,2).forEach(函数(二){
选择(C组,3).forEach(函数(C){
选(D组,1).forEach(功能(D){
的console.log(一个++ B [0] ++ B [1] ++ C [0] ++ C [1] ++ C [2] ++ D [0]);
});
});
});
});
输出示例:
A1 B1,B2,C1 C2 C3 D1
A2 B1,B2,C1 C2 C3 D1
要看到它的工作的一个例子,你可以这样做:
VAR testGroup = makeGroup('T',4);
VAR选中1 =选择(testGroup,1);
VAR choose2 =选择(testGroup,2);
VAR choose3 =选择(testGroup,3);
的console.log(JSON.stringify(选中1));
的console.log(JSON.stringify(choose2));
的console.log(JSON.stringify(choose3));
和看到,它选择正确的:
[T1],[T2],[T3],[T4]
[[T1,T2],[T1,T3],[T1,T4],[T2,T3],[T2,T4] [T3,T4]
[[T1,T2,T3],[T1,T2,T4],[T1,T3,T4],[T2,T3 ,T4]]
I have a 4 collections.
{ A1, A2, A3, ...}
{ B1, B1, B3, ...}
{ C1, C2, C3, ...}
{ D1, D2, D3, ...}
I need to find all of the possible combinations using the following rules:
- 1 item from the first and fourth collections
- 2 items from the second collection
- 3 items from the third
Example combinations:
{A1, B1, B2, C1, C2, C3, D1}
{A2, B1, B2, C1, C2, C3, D1}
解决方案
Visual Studio was taking too long to load, so I did it in JavaScript since I could test in my console. This will print out all the choices. (Also it sounds more like a "what's the algorithm for this?" not "what's the algorithm for this in C#?")
function makeGroup(id, count) {
var result = [];
for (var i = 1; i <= count; i++) {
result.push(id + i);
}
return result;
}
function choose(group, count) {
if (count === 1) {
var result = [];
for (var i = 0; i < group.length; i++) {
result.push([group[i]]);
}
return result;
}
if (count === 2) {
var result = [];
for (var i = 0; i < group.length; i++) {
for (var j = 0; j < group.length; j++) {
if (i !== j &&
i < j) {
result.push([group[i], group[j]]);
}
}
}
return result;
}
if (count === 3) {
var result = [];
for (var i = 0; i < group.length; i++) {
for (var j = 0; j < group.length; j++) {
for (var k = 0; k < group.length; k++) {
if (i !== j && i !== k && j !== k &&
i < j && j < k) {
result.push([group[i], group[j], group[k]]);
}
}
}
}
return result;
}
}
var groupA = makeGroup('A', 2);
var groupB = makeGroup('B', 2);
var groupC = makeGroup('C', 3);
var groupD = makeGroup('D', 1);
choose(groupA, 1).forEach(function (a) {
choose(groupB, 2).forEach(function (b) {
choose(groupC, 3).forEach(function (c) {
choose(groupD, 1).forEach(function (d) {
console.log(a + " " + b[0] + " " + b[1] + " " + c[0] + " " + c[1] + " " + c[2] + " " + d[0]);
});
});
});
});
Example output:
A1 B1 B2 C1 C2 C3 D1
A2 B1 B2 C1 C2 C3 D1
To see an example of it working, you can do:
var testGroup = makeGroup('T', 4);
var choose1 = choose(testGroup, 1);
var choose2 = choose(testGroup, 2);
var choose3 = choose(testGroup, 3);
console.log(JSON.stringify(choose1));
console.log(JSON.stringify(choose2));
console.log(JSON.stringify(choose3));
And see that it chooses correctly:
[["T1"],["T2"],["T3"],["T4"]]
[["T1","T2"],["T1","T3"],["T1","T4"],["T2","T3"],["T2","T4"],["T3","T4"]]
[["T1","T2","T3"],["T1","T2","T4"],["T1","T3","T4"],["T2","T3","T4"]]
这篇关于列表组合C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文