0

我有 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

        */
    }
}
4

3 回答 3

3

你不能做这个。

90!约为 1.49 * 10^138

假设您每秒可以处理 10 亿次排列,这将花费超过 4 * 10^112 亿年的时间。是当前宇宙年龄的很多很多倍。

玩得开心!:)

于 2013-02-19T10:23:08.060 回答
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;
}
于 2013-02-19T10:23:52.357 回答
0

根据KwCombinatorics库的 XML 中的文档,Permute方法抛出一个ArgumentOutOfRangeException When length of source is less than Kw.Combinatorics.Permutation.Width

于 2013-02-19T10:15:44.657 回答