C中的递归,理解递归示例 [英] Recursion in C, understand recursion example

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

问题描述

我无法理解这个例子.我无法弄清楚在某个时间点之后实际会发生什么.

i am having trouble understanding this example. I cant figure out what actually happens after a certain point.

这是代码,结果应该是4.

Here is the code, the result is supposed to be 4.

我可以看到它多次调用自己,但它实际上是如何得出 4 的结果的,我完全不明白.任何帮助将不胜感激.

I can see that it calls itself several times but how it actually comes to the result of 4 eludes me entirely. Any help would be greatly appreciated.

#include <stdio.h>

int recursion(int i) 
{ 
  return (i>1 ? i - recursion(i/2) : 3);
} 

int main() 
{ 
  int number = 9; 
  printf("The result is %d\n", recursion(number)); 
  return 0;
}

非常感谢,这就清除了!

edit: Thanks so much, that clears it up!

推荐答案

Here from code, recursion(1) = 3 and i/2 when i>;1 , 9/2 = 4 (因为 int 作为参数)

Here from code, recursion(1) = 3 and i/2 when i>1 , 9/2 = 4 (since int as parameter)

这个递归函数的基本条件是 i = 1

The base condition of this recursive function is when i = 1

这篇关于C中的递归,理解递归示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆