我有90的列表,列表中的每个元素都是600的int数组。 现在我想在这个列表上执行排列(而不是在int数组上),即我希望得到这个90个元素列表的所有可能的唯一组合,简而言之90!列表。
我正在使用kwCombinatorics
库。
这是代码。
第一个foreach声明抛出了这个异常,
ArgumentOutOfRangeException
-
值大于允许的最大值。
foreach(var row in new Permutation(image_matrix_90_600.Count).GetRows())
{
foreach(var mix in Permutation.Permute(row, image_matrix_90_600))
{
// code for saving the individual list to text.
}
}
她是http://kwcombinatorics.codeplex.com/
的例子using Kw.Combinatorics;
using System;
using System.Collections.Generic;
namespace Kw.CombinatoricsExamples
{
public class Furniture
{
private string name;
public Furniture (string newName) { name = newName; }
public override string ToString () { return name; }
}
public class Fruit
{
private string name;
public Fruit (string newName) { name = newName; }
public override string ToString () { return name; }
}
class PnExample03
{
static void Main ()
{
var things = new List<object>
{
new Fruit ("apple"),
new Furniture ("bench"),
new Furniture ("chair")
};
// Use permutations to get rearrangements of other objects:
foreach (var row in new Permutation (things.Count).GetRows())
{
foreach (var mix in Permutation.Permute (row, things))
Console.Write ("{0} ", mix);
Console.WriteLine ();
}
}
/* Output:
apple bench chair
apple chair bench
bench apple chair
bench chair apple
chair apple bench
chair bench apple
*/
}
}
答案 0 :(得分:3)
你不能这样做。
90!约为1.49 * 10 ^ 138
假设你每秒可以以某种方式处理10亿个排列,这将需要超过4 * 10 ^ 112亿年。现在是宇宙当前时代的很多倍。
玩得开心! :)
答案 1 :(得分:2)
构造函数width
中使用的Permutation(int width)
参数可能取0到20之间的值。可能你传入的image_matrix_90_600.Count
的值是90,这就是为什么你得到的异常。
/// <summary>
/// Make a new <see cref="Permutation"/> from the supplied
/// <em>width</em> of <see cref="Rank"/> 0.
/// </summary>
/// <param name="width">Number of elements of new sequence.</param>
/// <example>
/// <code source="Examples\Permutation\PnExample01\PnExample01.cs" lang="cs" />
/// </example>
/// <exception cref="ArgumentOutOfRangeException">
/// When <em>width</em> is less than 0 or greater than 20.
/// </exception>
public Permutation (int width)
{
if (width < 0)
throw new ArgumentOutOfRangeException ("width", "Value is less than zero.");
if (width > MaxWidth)
throw new ArgumentOutOfRangeException ("width", "Value is greater than maximum allowed.");
this.data = new int[width];
for (int ei = 0; ei < width; ++ei)
this.data[ei] = ei;
this.rank = 0;
}
答案 2 :(得分:0)
根据KwCombinatorics
库的XML文档,Permute
方法抛出ArgumentOutOfRangeException
当源的长度小于Kw.Combinatorics.Permutation.Width