递归的问题 怎样跑的?

念言 2013-04-25 10:42:54
#include<iostream>
using namespace std;

void main()
{
void combination( int m, int n, int start= 1, int deep= 0);
void permutation( int m, int deep= 0);
int m,n;
cin >>m >>n;
combination( m, n);
cin >>m;
permutation( m);
}

void permutation( int m, int deep= 0)//排列
{
static int Array[1000];
static bool Flag[1000];

if ( deep == m) //递归结束判断
{
for( int i= 0; i< deep; i++)
cout<< Array[i];
cout<< " ";
return ;
}
for (int i= 1; i <=m ;i++)
{
if( Flag[i]== true)
continue; //该数用过不用
Array[deep]= i;
Flag[i]= true;
permutation( m, deep+1);
Flag[i]= false;
}
if( deep == 0) //递归程序最终结束时换行
cout<< endl;
}

void combination( int m, int n, int start= 1, int deep= 0)//组合
{
static int Array[1000];
if( n == 0) //递归结束判断
{
for( int i= 0; i < deep; i++) //输出
cout<< Array[i];
cout<< " ";
return; //跳出递归
}
for( int i= start; i <= m-n+1; i++)
{
Array[deep]= i;
combination( m, n-1, i+1, deep+1);
}
if( deep == 0)//递归程序最终结束时换行
cout<< endl;
}

例如 输入 3 2 输出 12 13 23 输入3 输出 123 132 213 231 312 321这个combination函数是怎样跑的? 我怎么感觉 只能跑出一个数组(12) 然后就返回了,,下面的 2个数组 是怎样循环输出的?
...全文
113 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-04-26
  • 打赏
  • 举报
回复
“给定一个小点的输入,完整单步跟踪(同时按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史)一遍。”是理解递归函数工作原理的不二法门! 递归函数关注以下几个因素 ·退出条件 ·参数有哪些 ·返回值是什么 ·局部变量有哪些 ·全局变量有哪些 ·何时输出 ·会不会导致堆栈溢出
starytx 2013-04-26
  • 打赏
  • 举报
回复
在递归函数递归的地方打上断点(F9),然后按F5调试运行,在Watch窗口填写某个变量的名字,即可看到该变量当前的值
念言 2013-04-26
  • 打赏
  • 举报
回复
引用 1 楼 wuyg 的回复:
跟踪一下不就知道了嘛
跟踪???没学过,,
wuyg719 2013-04-25
  • 打赏
  • 举报
回复
跟踪一下不就知道了嘛

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧