C# 手动循环。斯科特:每个输出应该有10项,所以我认为你需要10项。你必须提供你在中间做的代码来说服我……我直接复制了这个,当我运行它时,它似乎永远在运行,这是我应该预料到的。不,根本不需要,在控制台上打印1048576行可能需要一段时间,但是它会停止。尝试在
C# 手动循环。斯科特:每个输出应该有10项,所以我认为你需要10项。你必须提供你在中间做的代码来说服我……我直接复制了这个,当我运行它时,它似乎永远在运行,这是我应该预料到的。不,根本不需要,在控制台上打印1048576行可能需要一段时间,但是它会停止。尝试在,c#,permutation,combinations,cartesian-product,C#,Permutation,Combinations,Cartesian Product,手动循环。斯科特:每个输出应该有10项,所以我认为你需要10项。你必须提供你在中间做的代码来说服我……我直接复制了这个,当我运行它时,它似乎永远在运行,这是我应该预料到的。不,根本不需要,在控制台上打印1048576行可能需要一段时间,但是它会停止。尝试在maxDigits中使用6运行它。如果删除控制台,则不需要花费任何时间。编写调用,即使使用maxDigits中的10。添加了choice变量以突出显示选择的数组中的哪个元素。好的,我想我已经开始考虑这个问题了,所以如果我想精确地生成104857
手动循环。斯科特:每个输出应该有10项,所以我认为你需要10项。你必须提供你在中间做的代码来说服我……我直接复制了这个,当我运行它时,它似乎永远在运行,这是我应该预料到的。不,根本不需要,在控制台上打印1048576行可能需要一段时间,但是它会停止。尝试在
maxDigits
中使用6
运行它。如果删除控制台,则不需要花费任何时间。编写调用,即使使用maxDigits
中的10
。添加了choice
变量以突出显示选择的数组中的哪个元素。好的,我想我已经开始考虑这个问题了,所以如果我想精确地生成1048576个组合,我会将其设置为max,然后设置baseN=10,最大数字=7。我这样做对吗?
var choices = { 1: {'Q': 100, 'R': 150, 'W' : 250, 'T', 30},
2: {'Q': 90, 'R': 130, 'W' : 225, 'T', 28},
3: {'Q': 80, 'R': 110, 'W' : 210, 'T', 25},
4: {'Q': 70, 'R': 90, 'W' : 180, 'T', 22},
5: {'Q': 60, 'R': 70, 'W' : 150, 'T', 18},
6: {'Q': 50, 'R': 50, 'W' : 110, 'T', 15},
7: {'Q': 40, 'R': 30, 'W' : 80, 'T', 11},
8: {'Q': 30, 'R': 25, 'W' : 50, 'T', 8},
9: {'Q': 20, 'R': 10, 'W' : 25, 'T', 5},
10: {'Q': 10, 'R': 5, 'W' : 15, 'T', 3}
};
var allChoices = { 0: {1: {'Q': 100},
2: {'R': 130},
3: {'W' : 210},
4: {'W' : 180},
5: {'T', 18},
6: {'R': 50,},
7: {'Q': 40,},
8: {'T', 8},
9: {'R': 10},
10: {'W' : 15},
},
1: {...},
...
1048576: {...}
};
foreach ( choice in allChoices )
{
foreach ( choice in allChoices )
{
foreach ( choice in allChoices )
{
foreach ( choice in allChoices )
{
// combine and add to a collection
}
}
}
}
class Program
{
static void Main(string[] args)
{
int baseN = 4;
int maxDigits = 10;
var max = Math.Pow(baseN, maxDigits);
for (int i = 0; i < max; i++)
{ // each iteration of this loop is another unique permutation
var digits = new int[maxDigits];
int value = i;
int place = digits.Length - 1;
while (value > 0)
{
int thisdigit = value % baseN;
value /= baseN;
digits[place--] = thisdigit;
}
int choice = 0;
foreach (var digit in digits)
{
choice ++;
//Console.Write(digit);
switch (digit)
{
case 0: break; //choose Q from choice
case 1: break; //choose R from choice
case 2: break; //choose W from choice
case 3: break; //choose T from choice
}
}
//Console.WriteLine();
// add it to your list of all permutations here
}
Console.WriteLine("Done")
Console.ReadLine();
}
}
static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences)
{
IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() };
return sequences.Aggregate(
emptyProduct,
(accumulator, sequence) =>
from accseq in accumulator
from item in sequence
select accseq.Concat(new[] {item}));
}
void Main()
{
var OriginValues = new List<KeyValuePair<char, int>>();
OriginValues.Add(new KeyValuePair<char, int>('Q', 100));
OriginValues.Add(new KeyValuePair<char, int>('R', 150));
OriginValues.Add(new KeyValuePair<char, int>('W', 250));
OriginValues.Add(new KeyValuePair<char, int>('T', 30));
OriginValues.Add(new KeyValuePair<char, int>('Q', 90));
OriginValues.Add(new KeyValuePair<char, int>('R', 130));
OriginValues.Add(new KeyValuePair<char, int>('W', 225));
OriginValues.Add(new KeyValuePair<char, int>('T', 28));
OriginValues.Add(new KeyValuePair<char, int>('Q', 80));
OriginValues.Add(new KeyValuePair<char, int>('R', 110));
OriginValues.Add(new KeyValuePair<char, int>('W', 210));
OriginValues.Add(new KeyValuePair<char, int>('T', 25));
///... and the other 7
var AllPermutation = new List<List<KeyValuePair<char, int>>>();
Recurse(OriginValues, ref AllPermutation);
//all results will be in AllPermutation now
}
const int PAIRCOUNT = 4;
void Recurse(List<KeyValuePair<char, int>> OriginValues, ref List<List<KeyValuePair<char, int>>> result, List<KeyValuePair<char, int>> itemset = null)
{
itemset = itemset ?? new List<KeyValuePair<char, int>>();
var temp = new List<KeyValuePair<char, int>>(itemset);
if (itemset.Count == OriginValues.Count / PAIRCOUNT)
{
result.Add(temp);
return;
}
for (int x = 0; x < PAIRCOUNT; x++)
{
temp.Add(OriginValues[itemset.Count * PAIRCOUNT + x]);
Recurse(OriginValues, ref result, temp);
temp = new List<KeyValuePair<char, int>>(itemset);
}
}