Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7563573
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: Java

2012-04-02 22:51:02


点击(此处)折叠或打开

  1. /**
  2.  *     递归调用
  3.  * Lzy    2012-4-2
  4.  */
  5.  public class Main
  6.  {
  7.     
  8.      public static void main(String args[])
  9.      {
  10.          System.out.println(method(5));
  11.      }
  12.     
  13.      public static int method(int n)
  14.      {
  15.          if(n == 1)
  16.              return 1;
  17.          else
  18.             return n * method(n-1);
  19.      }         
  20.  }

点击(此处)折叠或打开

  1. /* 不使用递归方法实现Fibonacci数列 */
  2. public class Fab {
  3.     public static void main(String[] args) {
  4.         System.out.println(f(-9));
  5.     }
  6.     
  7.     public static long f(int index) {
  8.         if(index < 1) {
  9.             System.out.println("invalid parameter!");
  10.             return -1;
  11.         }
  12.         
  13.         if(index == 1 || index == 2) {
  14.             return 1;
  15.         }
  16.         
  17.         long f1 = 1L;
  18.         long f2 = 1L;
  19.         long f = 0;
  20.         
  21.         for(int i=0; i<index-2; i++) {
  22.             f = f1 + f2;
  23.             f1 = f2;
  24.             f2 = f;
  25.             
  26.         }
  27.         
  28.         return f;
  29.     }
  30. }


课件: 递归调用.pdf   
阅读(1058) | 评论(0) | 转发(2) |
0

上一篇:Java 数据类型

下一篇:Java 基本数据类型

给主人留下些什么吧!~~