java 递归终止_java关于递归

递归函数,即是调用一个函数的过程中出现直接或简介的调用该函数本身.此种函数就称为递归函数

递归函数的优点是程序简洁易懂,可读性强;缺点是需要调用大量的函数调用,消耗大量的内存和时间

一般来说,递归由函数出口和递归体两部分组成,递归出口给出了递归终止条件,递归体给出了递归的方式.

下面一例就是简单的递归:

求N的阶乘,即求1乘2乘3一直乘到N的乘积.

递归形式如下:

f(1)=1

f(n)=f(n-1)*n

前者就是递归的出口,后者就是递归体

程序执行过程为:求f(4)反推到f(3),再反推到f(3),接着反推到f(2),最后反推到f(1),此时遇到递归出口,计算出f(1)=1,然后依次反推.f(2)=2*f(1)=2,f(3)=3*f(2)=3*2=6,f(4)=4*f(3)=4*6=24

程序代码实现:

import javax.swing.JOptionPane;

public class N {

static int f(int x){

int s;

if(x==1)

s=1;

else

s=f(x-1)*x;

return s;

}

public static void main(String[] args) {

int n;

N f=new N();

String s=JOptionPane.showInputDialog(null,"please input n:\n");

n=Integer.parseInt(s);

S

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/** * * @author SunnyMoon */ ////////////////////////////////////////////////////////////////////////////// /** * 概念介绍: * * 消除归: * 一个算法作为一个归的方法通常从概念上很容易理解,但实际使用中归的效率不高,在这种 * 情况下,把归算法转换成非归的算法是非常有用的,这种转换经常用到栈。 * * 归和栈: * 归和栈之间有着紧密的联系,大部分的编译器使用栈实现归的。 * * 调用方法的时候发生什么: * 1. 编译器会把这个方法所有当前参数及返回地址压入栈中; * 2. 将控制权交给这个方法,方法通过获得栈顶元素值访问参数; * 3. 方法运行结束的时候,值退栈,参数消失且控制权重新回到返回地址; * * 模拟归方法: * 可以将任意一个归方法转换为非归的基于栈的方法。在一些简单的情况可以完全消除栈,只 * 使用一个简单的循环,但是在很复杂的情况,算法中必须须要保留栈。本例子是简单的情况,可 * 以进一步完全消除栈。 */ ///////////////////////////////////////////////////////////////////////////// /** * 计算三角数字的问题: * 归算法描述如下 * int triiangle(int n){ * if(n==1) * return 1; * else * return (n+triangle(n-1)); * } */ import java.io.*; /** * 模拟一个归方法,通用的方式消除归 */ class Params {//封装了方法的返回地址和方法的参数 public int number; public int returnAddress; public Params(int num, int returnAdd) { number = num; returnAddress = returnAdd; } } class StackX {//模拟归时使用的栈 private int maxSize; private Params[] stackArray; private int top; public StackX(int s) { maxSize = s; stackArray = new Params[maxSize]; top = -1; } public void push(Params p) { stackArray[++top] = p; } public Params pop() { return stackArray[top--]; } public Params peek() { return stackArray[top]; } } class StackTriangleApp { static int theNumber; static int theAnswer; static StackX theStack; static int logicAddress; static Params theseParams; public static void main(String[] args) throws IOException{//主方法 System.out.print("Number = "); theNumber = getInt(); stackTriangle(); System.out.println(""); System.out.println("Trriangle = " + theAnswer); } @SuppressWarnings("empty-statement") public static void stackTriangle() {//计算三角数字的方法,模拟归方法 theStack = new StackX(100); logicAddress = 1;//设置一个逻辑地址为入口地址 while (step() == false); } public static boolean step() { switch (logicAddress) { case 1: theseParams = new Params(theNumber, 6);//设定循环返回的地址 theStack.push(theseParams); logicAddress = 2; break; case 2: theseParams = theStack.peek(); if (theseParams.number == 1) { theAnswer = 1; logicAddress = 5; } else { logicAddress = 3; } break; case 3: Params newParams = new Params(theseParams.number - 1, 4); theStack.push(newParams); logicAddress = 2; break; case 4: theseParams = theStack.peek(); theAnswer = theAnswer + theseParams.number; logicAddress = 5; break; case 5: theseParams = theStack.peek(); logicAddress = theseParams.returnAddress; theStack.pop(); break; case 6: return true; } return false; } public static String getString() throws IOException{ InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = br.readLine(); return s; } public static int getInt() throws IOException{ String s=getString(); return Integer.parseInt(s); } } /** * 总结: * 当要求效率的时候可以把弟归转化为基于栈的非归,进而可以把基于栈的转化为仅有循环的 * 非归,这种情况下效率是最高的。 * 但是一些复杂的情况可以转化为基于栈的非归,但是无法消除栈的。 * 一些归的算法是非常优秀的,比如分治算法。 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值