#include<stdio.h>
int fun(int x)
{
if(x/2>0)
fun(x/2);
printf("%d*",x);
}
int main()
{
fun(6);
}****
6/2 = 3 > 0 执行fun(3) x = 6
3 / 2 = 1 > 0执行fun(1) x = 3
1 / 2 = 0 x = 1
输出1*
回到上一步输出3*
回到上一步输出6*
结束
理解好函数调用的顺序就简单了
#include<bits/stdc++.h>
using namespace std;
int n,m;
int fun(int x)
{
n++;
cout<<"第"<<n<<"次进入:x="<<x<<endl;
if(x/2>0)
fun(x/2);
m++;
cout<<"第"<<m<<"次输出:x="<<x<<endl;
printf("%d*",x);
cout<<endl;
}
int main() {
fun(6);
}
你把这代码粘进编译器里,设置个断点,一步一步跟,就知道了。
看别人说100次不如自己动手做一做