首頁>Program>source

我有一个ArrayList [] myList,我試圖建立一个陣列中所有值排列的列表。

示例:(所有值都是字元串)

myList[0] = { "1", "5", "3", "9" };
myList[1] = { "2", "3" };
myList[2] = { "93" };

myList的計數可以變化,因此其长度事先未知.

我希望能够生成与以下類似的所有排列的列表(但具有一些其他格式)。

1 2 93
1 3 93
5 2 93
5 3 93
3 2 93
3 3 93
9 2 93
9 3 93

這是否使我想完成的事情有意義? 我似乎無法想出一種好的方法(如果有的话)。

編輯:
我不確定遞迴是否会干扰我以自己的方式格式化輸出的愿望.抱歉,在格式化之前我没有提到。

我想最终構建一个遵循以下格式的所有組合的string []陣列:

用於" 1 2 93"置換

我希望輸出為" val0 = 1; val1 = 2; val2 = 93;"

我現在將尝試遞迴.谢谢DrJokepu

最新回復
  • 5月前
    1 #

    我很惊讶没有人發佈LINQ解決方案。

    from val0 in new []{ "1", "5", "3", "9" }
    from val1 in new []{ "2", "3" }
    from val2 in new []{ "93" }
    select String.Format("val0={0};val1={1};val2={2}", val0, val1, val2)
    

  • 5月前
    2 #

    遞迴解決方案

       static List<string> foo(int a, List<Array> x)
        {
            List<string> retval= new List<string>();
            if (a == x.Count)
            {
                retval.Add("");
                return retval;
            }
            foreach (Object y in x[a])
            {
                foreach (string x2 in foo(a + 1, x))
                {
                    retval.Add(y.ToString() + " " + x2.ToString());
                }
            }
            return retval;
        }
        static void Main(string[] args)
        {
            List<Array> myList = new List<Array>();
            myList.Add(new string[0]);
            myList.Add(new string[0]);
            myList.Add(new string[0]);
            myList[0] = new string[]{ "1", "5", "3", "9" };
            myList[1] = new string[] { "2", "3" };
            myList[2] = new string[] { "93" };
            foreach (string x in foo(0, myList))
            {
                Console.WriteLine(x);
            }
            Console.ReadKey();
        }
    

    請註意,通過將return更改為字元串列表列表並更改retval.add呼叫以使用列表而不是使用串聯,很容易返迴列表或陣列而不是字元串. / p>

    工作原理:

    這是经典的遞迴演算法.基本案例是 foo(myList.Count, myList) ,它返迴一个包含一个元素(空字元串)的List. n个字元串陣列s1,s2,...,sN的列表的排列等於以n-1个字元串陣列s2,...,sN的排列為前缀的sA1的每个成員.基本情况就是在這裏為sN的每个元素提供連線。

  • 5月前
    3 #

    我最近在我的一个專案中遇到了一个類似的問题,偶然發現了這个問题.我需要一个可以處理任意物件列表的非遞迴解決方案.這是我想出的.基本上,我正在為每个子列表形成一个列舉器列表,並以迭代方式递增它们。

    public static IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<IEnumerable<T>> lists)
    {
        // Check against an empty list.
        if (!lists.Any())
        {
            yield break;
        }
        // Create a list of iterators into each of the sub-lists.
        List<IEnumerator<T>> iterators = new List<IEnumerator<T>>();
        foreach (var list in lists)
        {
            var it = list.GetEnumerator();
            // Ensure empty sub-lists are excluded.
            if (!it.MoveNext())
            {
                continue;
            }
            iterators.Add(it);
        }
        bool done = false;
        while (!done)
        {
            // Return the current state of all the iterator, this permutation.
            yield return from it in iterators select it.Current;
            // Move to the next permutation.
            bool recurse = false;
            var mainIt = iterators.GetEnumerator();
            mainIt.MoveNext(); // Move to the first, succeeds; the main list is not empty.
            do
            {
                recurse = false;
                var subIt = mainIt.Current;
                if (!subIt.MoveNext())
                {
                    subIt.Reset(); // Note the sub-list must be a reset-able IEnumerable!
                    subIt.MoveNext(); // Move to the first, succeeds; each sub-list is not empty.
                    if (!mainIt.MoveNext())
                    {
                        done = true;
                    }
                    else
                    {
                        recurse = true;
                    }
                }
            }
            while (recurse);
        }
    }
    

  • 5月前
    4 #

    您可以使用factoradics生成排列的列舉.在MSDN上尝試本文以获取C#中的實現.

  • 5月前
    5 #

    無論您將多少个陣列添加到myList,這都將起作用:

           static void Main(string[] args)
            {
                string[][] myList = new string[3][];
                myList[0] = new string[] { "1", "5", "3", "9" };
                myList[1] = new string[] { "2", "3" };
                myList[2] = new string[] { "93" };
                List<string> permutations = new List<string>(myList[0]);
                for (int i = 1; i < myList.Length; ++i)
                {
                    permutations = RecursiveAppend(permutations, myList[i]);
                }
                //at this point the permutations variable contains all permutations
            }
            static List<string> RecursiveAppend(List<string> priorPermutations, string[] additions)
            {
                List<string> newPermutationsResult = new List<string>();
                foreach (string priorPermutation in priorPermutations)
                {
                    foreach (string addition in additions)
                    {
                        newPermutationsResult.Add(priorPermutation + ":" + addition);
                    }
                }
                return newPermutationsResult;
            }
    

    請註意,它並不是真正的遞迴.可能是誤匯的函式名稱。

    這是一个符合您新要求的版本.請註意我輸出到控製台的部分,在這裏您可以进行自己的格式化:

    static void Main(string[] args)
            {
                string[][] myList = new string[3][];
                myList[0] = new string[] { "1", "5", "3", "9" };
                myList[1] = new string[] { "2", "3" };
                myList[2] = new string[] { "93" };
                List<List<string>> permutations = new List<List<string>>();
                foreach (string init in myList[0])
                {
                    List<string> temp = new List<string>();
                    temp.Add(init);
                    permutations.Add(temp);
                }
                for (int i = 1; i < myList.Length; ++i)
                {
                    permutations = RecursiveAppend(permutations, myList[i]);
                }
                //at this point the permutations variable contains all permutations
                foreach (List<string> list in permutations)
                {
                    foreach (string item in list)
                    {
                        Console.Write(item + ":");
                    }
                    Console.WriteLine();
                }
            }
            static List<List<string>> RecursiveAppend(List<List<string>> priorPermutations, string[] additions)
            {
                List<List<string>> newPermutationsResult = new List<List<string>>();
                foreach (List<string> priorPermutation in priorPermutations)
                {
                    foreach (string addition in additions)
                    {
                        List<string> priorWithAddition = new List<string>(priorPermutation);
                        priorWithAddition.Add(addition);
                        newPermutationsResult.Add(priorWithAddition);
                    }
                }
                return newPermutationsResult;
            }
    

  • c++:什麼是glibc free / malloc / realloc無效的下一个大小/無效的指標錯誤以及如何解決?
  • ruby:在Rails中使用attr_accessor