kbhao7788 2017-02-13 01:12 采纳率: 0%
浏览 2992
已采纳

c++ 递归函数的返回上一层怎么解释

#include
using namespace std;
int repeat(int m)
{
cout << m;
if (m>1)
{

    repeat(--m);
    cout << m << endl;
    m--;
}
return 1;

}
int main()
{

int h = 5;
repeat(h);

}

递归到终 repeat()函数有return1 返回到repeat(--m) repeat(--m)有return 1
为什么不返回repeat(int m) 而是向下走 求详细的答案谢谢

  • 写回答

2条回答 默认 最新

  • Xefvan 2017-02-13 02:31
    关注
     ------Start------
        |repeat(5)
        |---cout << 5;
        |---repeat(4)
        |   |---cout << 4;
        |   |---repeat(3)
        |   |   |---cout << 3;
        |   |   |---repeat(2)
        |   |   |   |---cout << 2;
        |   |   |   |---repeat(1)
        |   |   |   |   |---cout << 1;
        |   |   |   |   |---return 1;   // 退出
        |   |   |   |---cout << 1 << endl;
        |   |   |   |---m--;
        |   |   |   |---return 1;       // 退出
        |   |   |---cout << 2 << endl;
        |   |   |---m--;
        |   |   |---return 1;           // 退出
        |   |---cout << 3 << endl;
        |   |---m--;
        |   |---return 1;       // 退出
        |---cout << 4 << endl;
        |---m--;
        |---return 1;       // 退出
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 SQL server表计算问题
  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出