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

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

        C++ 阶乘程序中的递归

        时间:2023-08-03
        <legend id='NojZm'><style id='NojZm'><dir id='NojZm'><q id='NojZm'></q></dir></style></legend>

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

                    <tbody id='NojZm'></tbody>
                • <tfoot id='NojZm'></tfoot>

                  本文介绍了C++ 阶乘程序中的递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  你好,我有一段基于其他递归和阶乘程序编写的代码但我的问题是我真的很困惑它是如何存储值并保留它然后在最后返回它

                  hello i have this piece of code that i coded based on some other recursion and factorial programs but my problem is that i am really confused as to how it stored the value and kept it and then returned it at the end

                  int factorialfinder(int x)
                  {
                      if (x == 1)
                      {
                          return 1;
                      }else
                      {
                          return x*factorialfinder(x-1);
                      }
                  }
                  int main()
                  {
                    cout << factorialfinder(5) << endl;
                  }
                  

                  所以 5 进入,通过一次又一次地调用它的函数被乘以 4,然后它得到 1 并返回阶乘答案

                  so 5 goes in, and gets multiplied by 4 by calling its function again and again and again, then it gets to one and it returns the factorial answer

                  为什么?我不知道它是如何存储的,为什么 return 1 返回实际答案,它到底在做什么?

                  why? i have no idea how it got stored, why is return 1 returning the actual answer, what is it really doing?

                  推荐答案

                  来源:图片取自:IBM 开发人员网站

                  看看上面的图片,你会更好地理解它.该数字永远不会被存储,而是被递归调用以计算输出.

                  Just take a look at the picture above, you will understand it better. The number never gets stored, but gets called recursively to calculate the output.

                  因此,当您调用 fact(4) 时,当前堆栈用于存储每个参数,因为递归调用发生到 factorialfinder(1).所以计算是这样的:5*4*3*2*1.

                  So when you call the fact(4) the current stack is used to store every parameter as the recursive calls occur down to factorialfinder(1). So the calculation goes like this: 5*4*3*2*1.

                  int factorialfinder(int x)         
                  {
                      if (x == 1)        // HERE 5 is not equal to 1 so goes to else
                      {
                          return 1;
                      }else
                      {
                          return x*factorialfinder(x-1); // returns 5*4*3*2*1  when x==1 it returns 1
                      }
                  }
                  

                  希望这会有所帮助.

                  这篇关于C++ 阶乘程序中的递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:lambda 函数可以递归吗? 下一篇:编写一个递归函数来反转输入字符串

                  相关文章

                  最新文章

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

                  <tfoot id='W79nv'></tfoot>
                  1. <small id='W79nv'></small><noframes id='W79nv'>

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