码迷,mamicode.com
首页 > 其他好文 > 详细

递归算法——求取斐波那契数列(1)

时间:2014-07-15 00:39:15      阅读:408      评论:0      收藏:0      [点我收藏+]

标签:style   blog   java   color   art   for   

import java.util.Scanner;

/**
 * Created by Administrator on 14-5-13.
 * 计算斐波那契数列
 *
 * Result M(Problem prob)
 {
 if (<problem can be solved easily>)
 return <easy solution>;
 // The problem cannot be solved easily.
 Problem smaller1 = <reduce problem to smaller problem>
 Result result1 = M(smaller1);
 Problem smaller2 = <reduce problem to smaller problem>
 Result result2 = M(smaller2);
 ...
 Result finalResult = <combine all results of smaller problem to solve large problem>
 return finalResult;
 }
 */
public class Fib {
    public static void main(String[] args){
        long startTime=System.currentTimeMillis();   //获取开始时间
        int number=0;
        System.out.println("please give the number:");
        Scanner scanner=new Scanner(System.in);
        String str=scanner.nextLine();
        try{
            number=Integer.parseInt(str);
        }catch(NumberFormatException e){
            System.out.println("输入有误");
        }

        System.out.println(fac(number));
        long endTime=System.currentTimeMillis(); //获取结束时间
        System.out.println("程序运行时间: "+(endTime-startTime)+"ms");
    }
    public static int fac(int temp){
        if(temp<=2)
        {
            return 1;
        }
        else {
            return fac(temp-1)+fac(temp-2);
        }

    }
}

 

递归算法——求取斐波那契数列(1),布布扣,bubuko.com

递归算法——求取斐波那契数列(1)

标签:style   blog   java   color   art   for   

原文地址:http://www.cnblogs.com/mingcaoyouxin/p/3842110.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!