递归算法总结

来源:互联网 发布:淘宝网如何退货 编辑:程序博客网 时间:2024/04/27 23:08

递归算法总结

简单的案例

 

package com.temp;public class Jiecheng{    public static void main(String args[]) throws InterruptedException    {        long time1 = System.currentTimeMillis();                 jiecheng(5000);        System.out.println(System.currentTimeMillis()-time1);       //  Thread.sleep(1500);                long time2 = System.currentTimeMillis();                 jiechengNone(5000);        System.out.println(System.currentTimeMillis()-time2);    }    // 不适用递归的方法    public static long jiecheng(long number) throws InterruptedException    {        Thread.sleep(1);        if (number == 1)        {            return 1L;        }        return number * jiecheng(number - 1);    }    // 使用递归的方法    public static long jiechengNone(long num) throws InterruptedException    {        long temp = 1L;        for (long i = num; i > 1 ; i--)        {            Thread.sleep(1);            temp = temp * i;        }        return temp;    }}