{"id":103,"date":"2015-07-06T21:32:10","date_gmt":"2015-07-06T13:32:10","guid":{"rendered":"https:\/\/blog.freesilo.com\/?p=103"},"modified":"2015-07-06T21:32:10","modified_gmt":"2015-07-06T13:32:10","slug":"c%e6%b3%9b%e5%9e%8b%e6%8e%92%e5%88%97%e7%bb%84%e5%90%88%e6%a0%b8%e5%bf%83%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/freesilo.com\/?p=103","title":{"rendered":"C#\u6cdb\u578b\u6392\u5217\u7ec4\u5408\u6838\u5fc3\u7b97\u6cd5"},"content":{"rendered":"<pre class=\"theme:github toolbar:1 lang:c# decode:true \" title=\"C#\u6cdb\u578b\u6392\u5217\u7ec4\u5408\u6838\u5fc3\u7b97\u6cd5\">using System;\nusing System.Collections.Generic;\nusing System.Text;\nusing System.Collections;\n\nnamespace calu\n{\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            float[] arr = new float[3];\n            for (int i = 0; i &lt; arr.Length; i++)\n            {\n                arr[i] = i + 1;\n            }\n            \/\/\u6c42\u6392\u5217\n            List&lt;float[]&gt; lst_Permutation = PermutationAndCombination&lt;float&gt;.GetPermutation(arr,3);\n            \/\/\u5176\u4e2d\n            List&lt;float[]&gt; lst_Combination = PermutationAndCombination&lt;float&gt;.GetCombination(arr, 2);\n            Console.WriteLine(\"\u6392\u5217:\");\n            int icount = 1;\n            foreach (float[] array in lst_Permutation)\n            {                \n                Console.Write(\"\u6392\u52171\" + icount++ + \"--&gt;\");\n                foreach (float itemp in array)\n                {\n                    Console.Write(itemp.ToString() + \",\");\n                }\n                Console.WriteLine();               \n            }\n            Console.WriteLine(\"\u7ec4\u5408:\");\n            icount = 1;\n            foreach (float[] array in lst_Combination)\n            {\n                Console.Write(\"\u7ec4\u54081\" + icount++ + \"--&gt;\");\n                foreach (float itemp in array)\n                {\n                    Console.Write(itemp.ToString() + \",\");\n                }\n                Console.WriteLine();\n            }\n            Console.ReadKey();\n\n        }\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u6392\u5217\u7ec4\u5408\u7b97\u6cd5\n        \/\/\/ &lt;\/summary&gt;\n        \/\/\/ &lt;typeparam name=\"T\"&gt;&lt;\/typeparam&gt;\n        public static class PermutationAndCombination&lt;T&gt;\n        {\n            \/\/\/ &lt;summary&gt;\n            \/\/\/ \u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\n            \/\/\/ &lt;\/summary&gt;\n            \/\/\/ &lt;param name=\"a\"&gt;\u53d8\u91cf1&lt;\/param&gt;\n            \/\/\/ &lt;param name=\"b\"&gt;\u53d8\u91cf2&lt;\/param&gt;\n            public static void Swap(ref T a, ref T b)\n            {\n                T temp = a;\n                a = b;\n                b = temp;\n            }\n\n            \/\/\/ &lt;summary&gt;\n            \/\/\/ \u9012\u5f52\u7b97\u6cd5\u6c42\u6570\u7ec4\u7684\u7ec4\u5408(\u79c1\u6709\u6210\u5458)\uff0c\u6838\u5fc3\u7b97\u6cd5\n            \/\/\/ &lt;\/summary&gt;\n            \/\/\/ &lt;param name=\"list\"&gt;\u8fd4\u56de\u7684\u8303\u578b&lt;\/param&gt;\n            \/\/\/ &lt;param name=\"t\"&gt;\u6240\u6c42\u6570\u7ec4&lt;\/param&gt;\n            \/\/\/ &lt;param name=\"n\"&gt;\u8f85\u52a9\u53d8\u91cf&lt;\/param&gt;\n            \/\/\/ &lt;param name=\"m\"&gt;\u8f85\u52a9\u53d8\u91cf&lt;\/param&gt;\n            \/\/\/ &lt;param name=\"b\"&gt;\u8f85\u52a9\u6570\u7ec4&lt;\/param&gt;\n            \/\/\/ &lt;param name=\"M\"&gt;\u8f85\u52a9\u53d8\u91cfM&lt;\/param&gt;\n            private static void GetCombination(ref List&lt;T[]&gt; list, T[] t, int n, int m, int[] b, int M)\n            {\n                for (int i = n; i &gt;= m; i--)\n                {\n                    b[m - 1] = i - 1;\n                    if (m &gt; 1)\n                    {\n                        GetCombination(ref list, t, i - 1, m - 1, b, M);\n                    }\n                    else\n                    {\n                        if (list == null)\n                        {\n                            list = new List&lt;T[]&gt;();\n                        }\n                        T[] temp = new T[M];\n                        for (int j = 0; j &lt; b.Length; j++)\n                        {\n                            temp[j] = t[b[j]];\n                        }\n                        list.Add(temp);\n                    }\n                }\n            }\n\n            \/\/\/ &lt;summary&gt;\n            \/\/\/ \u9012\u5f52\u7b97\u6cd5\u6c42\u6392\u5217(\u79c1\u6709\u6210\u5458)\uff0c\u6838\u5fc3\u7b97\n            \/\/\/ &lt;\/summary&gt;\n            \/\/\/ &lt;param name=\"list\"&gt;\u8fd4\u56de\u7684\u5217\u8868&lt;\/param&gt;\n            \/\/\/ &lt;param name=\"t\"&gt;\u6240\u6c42\u6570\u7ec4&lt;\/param&gt;\n            \/\/\/ &lt;param name=\"startIndex\"&gt;\u8d77\u59cb\u6807\u53f7&lt;\/param&gt;\n            \/\/\/ &lt;param name=\"endIndex\"&gt;\u7ed3\u675f\u6807\u53f7&lt;\/param&gt;\n            private static void GetPermutation(ref List&lt;T[]&gt; list, T[] t, int startIndex, int endIndex)\n            {\n                if (startIndex == endIndex)\/\/\u53ea\u6709\u4e00\u79cd\u65b9\u6cd5\n                {\n                    if (list == null)\n                    {\n                        list = new List&lt;T[]&gt;();\n                    }\n                    T[] temp = new T[t.Length];\n                    t.CopyTo(temp, 0);\n                    list.Add(temp);\n                }\n                else\n                {\n                    for (int i = startIndex; i &lt;= endIndex; i++)\n                    {\n                        Swap(ref t[startIndex], ref t[i]);\n                        GetPermutation(ref list, t, startIndex + 1, endIndex);\n                        Swap(ref t[startIndex], ref t[i]);\n                    }\n                }\n            }\n\n            \/\/\/ &lt;summary&gt;\n            \/\/\/ \u6c42\u4ece\u8d77\u59cb\u6807\u53f7\u5230\u7ed3\u675f\u6807\u53f7\u7684\u6392\u5217\uff0c\u5176\u4f59\u5143\u7d20\u4e0d\u53d8\n            \/\/\/ &lt;\/summary&gt;\n            \/\/\/ &lt;param name=\"t\"&gt;\u6240\u6c42\u6570\u7ec4&lt;\/param&gt;\n            \/\/\/ &lt;param name=\"startIndex\"&gt;\u8d77\u59cb\u6807\u53f7&lt;\/param&gt;\n            \/\/\/ &lt;param name=\"endIndex\"&gt;\u7ed3\u675f\u6807\u53f7&lt;\/param&gt;\n            \/\/\/ &lt;returns&gt;\u4ece\u8d77\u59cb\u6807\u53f7\u5230\u7ed3\u675f\u6807\u53f7\u6392\u5217\u7684\u8303\u578b&lt;\/returns&gt;\n            public static List&lt;T[]&gt; GetPermutation(T[] t, int startIndex, int endIndex)\n            {\n                if (startIndex &lt; 0 || endIndex &gt; t.Length - 1)\n                {\n                    return null;\n                }\n                List&lt;T[]&gt; list = new List&lt;T[]&gt;();\n                GetPermutation(ref list, t, startIndex, endIndex);\n                return list;\n            }\n\n            \/\/\/ &lt;summary&gt;\n            \/\/\/ \u8fd4\u56de\u6570\u7ec4\u6240\u6709\u5143\u7d20\u7684\u5168\u6392\u5217\n            \/\/\/ &lt;\/summary&gt;\n            \/\/\/ &lt;param name=\"t\"&gt;\u6240\u6c42\u6570\u7ec4&lt;\/param&gt;\n            \/\/\/ &lt;returns&gt;\u5168\u6392\u5217\u7684\u8303\u578b&lt;\/returns&gt;\n            public static List&lt;T[]&gt; GetPermutation(T[] t)\n            {\n                return GetPermutation(t, 0, t.Length - 1);\n            }\n\n            \/\/\/ &lt;summary&gt;\n            \/\/\/ \u6c42\u6570\u7ec4\u4e2dn\u4e2a\u5143\u7d20\u7684\u6392\u5217\n            \/\/\/ &lt;\/summary&gt;\n            \/\/\/ &lt;param name=\"t\"&gt;\u6240\u6c42\u6570\u7ec4&lt;\/param&gt;\n            \/\/\/ &lt;param name=\"n\"&gt;\u5143\u7d20\u4e2a\u6570&lt;\/param&gt;\n            \/\/\/ &lt;returns&gt;\u6570\u7ec4\u4e2dn\u4e2a\u5143\u7d20\u7684\u6392\u5217&lt;\/returns&gt;\n            public static List&lt;T[]&gt; GetPermutation(T[] t, int n)\n            {\n                if (n &gt; t.Length)\n                {\n                    return null;\n                }\n                List&lt;T[]&gt; list = new List&lt;T[]&gt;();\n                List&lt;T[]&gt; c = GetCombination(t, n);\n                for (int i = 0; i &lt; c.Count; i++)\n                {\n                    List&lt;T[]&gt; l = new List&lt;T[]&gt;();\n                    GetPermutation(ref l, c[i], 0, n - 1);\n                    list.AddRange(l);\n                }\n                return list;\n            }\n\n            \/\/\/ &lt;summary&gt;\n            \/\/\/ \u6c42\u6570\u7ec4\u4e2dn\u4e2a\u5143\u7d20\u7684\u7ec4\u5408\n            \/\/\/ &lt;\/summary&gt;\n            \/\/\/ &lt;param name=\"t\"&gt;\u6240\u6c42\u6570\u7ec4&lt;\/param&gt;\n            \/\/\/ &lt;param name=\"n\"&gt;\u5143\u7d20\u4e2a\u6570&lt;\/param&gt;\n            \/\/\/ &lt;returns&gt;\u6570\u7ec4\u4e2dn\u4e2a\u5143\u7d20\u7684\u7ec4\u5408\u7684\u8303\u578b&lt;\/returns&gt;\n            public static List&lt;T[]&gt; GetCombination(T[] t, int n)\n            {\n                if (t.Length &lt; n)\n                {\n                    return null;\n                }\n                int[] temp = new int[n];\n                List&lt;T[]&gt; list = new List&lt;T[]&gt;();\n                GetCombination(ref list, t, t.Length, n, temp, n);\n                return list;\n            }\n        }\n\n    }\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>using System; using System.Collections.G&#8230;<br \/><a class=\"read-more-button\" href=\"https:\/\/freesilo.com\/?p=103\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-103","post","type-post","status-publish","format-standard","hentry","category-csharp"],"_links":{"self":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=103"}],"version-history":[{"count":0,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/103\/revisions"}],"wp:attachment":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}