什么是递归 [英] What is recursion

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

问题描述

可能重复:
递归函数示例

Possible Duplicate:
Examples of Recursive functions

我一直在尝试将编程递归作为一个概念(尽管我专门研究Java),而这正是我最好的理解:

I have been trying to research recursion in programming as a concept (though I am specifically studying Java) and this is what I got to understand the best:

例如,在现实生活中,递归是指将两个镜子彼此相对放置,并且在它们之间产生的图像是递归的.

In real life for example, recursion is when we put two mirrors infront of each other and the images produced between them are recursive.

但是我在编程中没有得到这个算法吗? 有人可以给我一个简化的例子来理解递归吗?

But I don't get this algorithm in programming? Can someone give me a simplified example to understand recursion ?

推荐答案

基本上,在以下情况下函数是递归的

Basically, a function is recursive when

  1. 函数具有简单的基本情况,以及何时
  2. 所有其他案例的规则都简化为基本案例.

例如,要计算阶乘:

public static long factorial(int i)
{
    // This is the base case
    if(i == 0)
    {
         return 1;
    }
    else
    {
        // This reduces the problem to something closer to the base case
        return i * factorial(i - 1);
    }
}

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

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