代码之家  ›  专栏  ›  技术社区  ›  oshirowanen

比你的正常排列还要多

  •  2
  • oshirowanen  · 技术社区  · 7 年前

    following example 用于生成置换的goes:

    namespace ConsoleApp1
    {
        class Program
        {
            public static void Main()
            {
                int n, i;
                formPermut test = new formPermut();
                int[] arr1 = new int[5];
    
                Console.WriteLine("\n\n Recursion : Generate all possible permutations of an array :");
                Console.WriteLine("------------------------------------------------------------------");
    
                Console.Write(" Input the number of elements to store in the array [maximum 5 digits ] :");
                n = Convert.ToInt32(Console.ReadLine());
                Console.Write(" Input {0} number of elements in the array :\n", n);
                for (i = 0; i < n; i++)
                {
                    Console.Write(" element - {0} : ", i);
                    arr1[i] = Convert.ToInt32(Console.ReadLine());
                }
    
                Console.Write("\n The Permutations with a combination of {0} digits are : \n", n);
                test.prnPermut(arr1, 0, n - 1);
                Console.Write("\n\n");
                Console.ReadKey();
            }
    
            class formPermut
            {
                public void swapTwoNumber(ref int a, ref int b)
                {
                    int temp = a;
                    a = b;
                    b = temp;
                }
                public void prnPermut(int[] list, int k, int m)
                {
                    int i;
                    if (k == m)
                    {
                        for (i = 0; i <= m; i++)
                            Console.Write("{0}", list[i]);
                        Console.Write(" ");
                    }
                    else
                        for (i = k; i <= m; i++)
                        {
                            swapTwoNumber(ref list[k], ref list[i]);
                            prnPermut(list, k + 1, m);
                            swapTwoNumber(ref list[k], ref list[i]);
                        }
                }
            }
        }
    }
    

    1 和 2 ,上述代码将返回以下结果 12 和 21

    1. 2. 21

    和 2. 和 3 ,在返回时:

    123 132 213 231 321 312

    有人知道我该怎么做吗?

    例如:

    1 2 3 12 13 21 23 31 32 123 132 213 321 312

    我的最终目标是能够做同样的事情,但是使用字符串,所以如果输入是 one 和 two ,则输出为:

    onetwo 二 twoone

    和 二 和 three

    产出将是:

    one two three onetwo onethree twoone twothree threeone threetwo onetwothree onethreetwo twoonethree threetwoone threeonetwo 假设我没有遗漏任何组合。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Elliveny    7 年前

    这似乎能解决问题?

    测试用例:

    输入:1,2给出。。。

    2 1 21 12 
    

    输入1,2,3给出。。。

    3 2 32 23 1 31 13 21 12 321 312 231 213 123 132 
    

    three two threetwo twothree one threeone onethree twoone onetwo threetwoone threeonetwo twothreeone twoonethree onetwothree onethreetwo 
    

    根据该代码:

    class Program
    {
        public static void Main()
        {
            formPermut test = new formPermut();
            test.prnPermutWithSubsets(new object[] { 1, 2 });
            Console.WriteLine();
            test.prnPermutWithSubsets(new object[] { 1, 2, 3 });
            Console.WriteLine();
            test.prnPermutWithSubsets(new string[] { "one", "two", "three" });
            Console.WriteLine();
            return;
        }
    
        class formPermut
        {
            private void swapTwoNumber(ref object a, ref object b)
            {
                object temp = a;
                a = b;
                b = temp;
            }
            public void prnPermutWithSubsets(object[] list)
            {
                for (int i = 0; i < Math.Pow(2, list.Length); i++)
                {
                    Stack<object> combination = new Stack<object>();
                    for (int j = 0; j < list.Length; j++)
                    {
                        if ((i & (1 << (list.Length - j - 1))) != 0)
                        {
                            combination.Push(list[j]);
                        }
                    }
                    this.prnPermut(combination.ToArray(), 0, combination.Count() - 1);
                }
            }
    
            public void prnPermut(object[] list, int k, int m)
            {
                int i;
                if (k == m)
                {
                    for (i = 0; i <= m; i++)
                        Console.Write("{0}", list[i]);
                    Console.Write(" ");
                }
                else
                    for (i = k; i <= m; i++)
                    {
                        swapTwoNumber(ref list[k], ref list[i]);
                        prnPermut(list, k + 1, m);
                        swapTwoNumber(ref list[k], ref list[i]);
                    }
            }
        }
    }
    
    推荐文章