列表的置换,其中每个元素都是int的数组
本文关键字:元素 int 数组 列表 | 更新日期: 2023-09-27 18:00:16
我有一个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
*/
}
}
您不能这样做。
90!约为1.49*10^138
假设你能以某种方式每秒处理10亿个排列,这将需要超过4*10^1120亿年的时间。许多,许多倍于当前宇宙时代。
玩得开心!:)
构造函数Permutation(int width)
中使用的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;
}
根据KwCombinatorics
库XML中的文档,当源的长度小于Kw时,Permute
方法抛出一个ArgumentOutOfRangeException
。Combinatorics.Permination.Width.