1 package com.wang.test;
2
3 /*
4 定义了一个这样的数列:0,1,1,2,3,5....(裴波那切数列)。要求:
5 使用递归方法获取第n数的数值
6 */
7 public class Recursion {
8 public static void main(String[] args) {
9 int f = f(10);
10 System.out.println(f); //输出55
11 }
12
13
14
15 public static int f(int n){
16 if(n<1){
17 return 0;
18 }else if(n==1 || n==2){
19 return 1;
20 }else{
21 return f(n-1)+f(n-2);
22 }
23 }
24 }