考虑以下递归C函数.
void get (int n) {
if (n<1) return;
get (n-1) ;
get (n-3) ;
printf ("%d", n) ;
}
Run Code Online (Sandbox Code Playgroud)
如果get(6)正在调用函数,main()那么get()在返回main之前调用函数的次数是多少次0?
要确定调用函数的次数,请在每次输入函数时递增静态计数器,并在调用后打印出值.例如:
int counter;
void get (int n) {
++counter; /* increment counter before first return */
if (n<1) return;
get (n-1) ;
get (n-3) ;
printf ("%d", n) ;
}
int main()
{
counter = 0; /* reset counter before each use */
get(6);
printf("get() was called %d times\n", counter);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1627 次 |
| 最近记录: |