<i id='bT9Jj'><tr id='bT9Jj'><dt id='bT9Jj'><q id='bT9Jj'><span id='bT9Jj'><b id='bT9Jj'><form id='bT9Jj'><ins id='bT9Jj'></ins><ul id='bT9Jj'></ul><sub id='bT9Jj'></sub></form><legend id='bT9Jj'></legend><bdo id='bT9Jj'><pre id='bT9Jj'><center id='bT9Jj'></center></pre></bdo></b><th id='bT9Jj'></th></span></q></dt></tr></i><div id='bT9Jj'><tfoot id='bT9Jj'></tfoot><dl id='bT9Jj'><fieldset id='bT9Jj'></fieldset></dl></div>

        <bdo id='bT9Jj'></bdo><ul id='bT9Jj'></ul>

      1. <small id='bT9Jj'></small><noframes id='bT9Jj'>

        <tfoot id='bT9Jj'></tfoot>

      2. <legend id='bT9Jj'><style id='bT9Jj'><dir id='bT9Jj'><q id='bT9Jj'></q></dir></style></legend>

        了解递归以生成排列

        时间:2023-08-03

        <small id='xi9uZ'></small><noframes id='xi9uZ'>

            <bdo id='xi9uZ'></bdo><ul id='xi9uZ'></ul>
            • <tfoot id='xi9uZ'></tfoot>
            • <legend id='xi9uZ'><style id='xi9uZ'><dir id='xi9uZ'><q id='xi9uZ'></q></dir></style></legend>
                <i id='xi9uZ'><tr id='xi9uZ'><dt id='xi9uZ'><q id='xi9uZ'><span id='xi9uZ'><b id='xi9uZ'><form id='xi9uZ'><ins id='xi9uZ'></ins><ul id='xi9uZ'></ul><sub id='xi9uZ'></sub></form><legend id='xi9uZ'></legend><bdo id='xi9uZ'><pre id='xi9uZ'><center id='xi9uZ'></center></pre></bdo></b><th id='xi9uZ'></th></span></q></dt></tr></i><div id='xi9uZ'><tfoot id='xi9uZ'></tfoot><dl id='xi9uZ'><fieldset id='xi9uZ'></fieldset></dl></div>
                  <tbody id='xi9uZ'></tbody>

                  本文介绍了了解递归以生成排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我发现递归,除了像阶乘这样非常直接的递归,非常难以理解.以下代码段打印字符串的所有排列.谁能帮我理解一下.正确理解递归的方法是什么.

                  I find recursion, apart from very straight forward ones like factorial, very difficult to understand. The following snippet prints all permutations of a string. Can anyone help me understand it. What is the way to go about to understand recursion properly.

                  void permute(char a[], int i, int n)
                  {
                     int j;
                     if (i == n)
                       cout << a << endl;
                     else
                     {
                         for (j = i; j <= n; j++)
                         {
                            swap(a[i], a[j]);          
                            permute(a, i+1, n);
                            swap(a[i], a[j]);
                         }
                     }
                  } 
                  
                  int main()
                  {
                     char a[] = "ABCD";
                     permute(a, 0, 3);
                     getchar();
                     return 0;
                  }
                  

                  推荐答案

                  PaulR 提出了正确的建议.您必须通过手工"(使用任何您想要的工具——调试器、文件、记录函数调用和某些点的变量)来运行代码,直到你理解它.有关代码的解释,我会向您推荐 quasiverse 的优秀答案.

                  PaulR has the right suggestion. You have to run through the code by "hand" (using whatever tools you want - debuggers, paper, logging function calls and variables at certain points) until you understand it. For an explanation of the code I'll refer you to quasiverse's excellent answer.

                  也许这个调用图的可视化用稍微小一点的字符串使它的工作原理更加明显:

                  Perhaps this visualization of the call graph with a slightly smaller string makes it more obvious how it works:

                  该图是使用 graphviz 制作的.

                  The graph was made with graphviz.

                  // x.dot
                  // dot x.dot -Tpng -o x.png
                  digraph x {
                  rankdir=LR
                  size="16,10"
                  
                  node [label="permute("ABC", 0, 2)"] n0;
                   node [label="permute("ABC", 1, 2)"] n1;
                    node [label="permute("ABC", 2, 2)"] n2;
                    node [label="permute("ACB", 2, 2)"] n3;
                   node [label="permute("BAC", 1, 2)"] n4;
                    node [label="permute("BAC", 2, 2)"] n5;
                    node [label="permute("BCA", 2, 2)"] n6;
                   node [label="permute("CBA", 1, 2)"] n7;
                    node [label="permute("CBA", 2, 2)"] n8;
                    node [label="permute("CAB", 2, 2)"] n9;
                  
                  n0 -> n1 [label="swap(0, 0)"];
                  n0 -> n4 [label="swap(0, 1)"];
                  n0 -> n7 [label="swap(0, 2)"];
                  
                  n1 -> n2 [label="swap(1, 1)"];
                  n1 -> n3 [label="swap(1, 2)"];
                  
                  n4 -> n5 [label="swap(1, 1)"];
                  n4 -> n6 [label="swap(1, 2)"];
                  
                  n7 -> n8 [label="swap(1, 1)"];
                  n7 -> n9 [label="swap(1, 2)"];
                  }
                  

                  这篇关于了解递归以生成排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C++ 中相互包含的头文件 下一篇:如何在 C++ 中创建多个向量的组合而无需硬编码循

                  相关文章

                  最新文章

                    <i id='pBI62'><tr id='pBI62'><dt id='pBI62'><q id='pBI62'><span id='pBI62'><b id='pBI62'><form id='pBI62'><ins id='pBI62'></ins><ul id='pBI62'></ul><sub id='pBI62'></sub></form><legend id='pBI62'></legend><bdo id='pBI62'><pre id='pBI62'><center id='pBI62'></center></pre></bdo></b><th id='pBI62'></th></span></q></dt></tr></i><div id='pBI62'><tfoot id='pBI62'></tfoot><dl id='pBI62'><fieldset id='pBI62'></fieldset></dl></div>

                    • <bdo id='pBI62'></bdo><ul id='pBI62'></ul>

                    <small id='pBI62'></small><noframes id='pBI62'>

                    <legend id='pBI62'><style id='pBI62'><dir id='pBI62'><q id='pBI62'></q></dir></style></legend>

                      <tfoot id='pBI62'></tfoot>