不死神兔

/**
* 使用递归计算斐波拉契数列数据得到第20个值并打印
* 不死神兔
*/
public class Rabbits {
public static void main(String[] args) {
int num = f(20);
System.out.println(num);
}

/**
* 定义方法计算兔子
*/
public static int f(int n){
if(n == 1 || n == 2){
return 1;
}else{
return f(n - 1) + f(n - 2);
}
}
}
posted @ 2019-12-10 20:44  YRSWBY  阅读(386)  评论(0)    收藏  举报