递归算法总结

递归算法总结

简单的案例

 

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;
    }
}


 

你可能感兴趣的:(编程杂技,java,编程技术,开发杂谈,递归算法)