简体   繁体   English

写一个返回指定斐波那契数的方法?

[英]Writing a method which returns the specified Fibonacci number?

The Fibonacci sequence is a set of Numbers where each number, after the first two, is the sum of the previous two numbers, resulting in the following sequence: 斐波那契数列是一组数字,其中前两个数字之后的每个数字都是前两个数字的总和,从而形成以下序列:

0 1 1 2 3 5 6 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 0 1 1 2 3 5 6 13 21 34 55 89 144 233 377 610 987 987 1597 2584 4181

How can I write a method, using recursion, which will return a specified fibonacci number? 如何编写使用递归的方法,该方法将返回指定的斐波那契数? I want to avoid using an array 我想避免使用数组

Here is what i have so far 这是我到目前为止所拥有的

public static int fibo(int n)
{
    if (n==1)
        return 1;
    else
        return //this is the part i am stuck on

As a certain fibonacci number (except 1) is on its percursor you call: 由于一定的斐波那契数(除1外)在您的名字上,您可以致电:

public static int fibo(int n) {
    if (n < 0) {
        throw new IndexOutOfBoundsException("Can't calculate fibonacci number for negative index");
    } else if(n == 0 || n == 1) {
        return n;
    }

    return fibo(n-1) + fibo(n-2);
}
public static int fibo(int n){ 
  if(n<=2)
    return (n-1);
  else 
    return fibo(n-1) + fibo(n-2);
}

Each fibonacci number is the sum of its 2 predecessors. 每个斐波那契数是其2个前任之和。 You can't just have a base case of n=1 since when calculating n = 2 you will find it is the sum of n = 1 and n = 0? 您不能仅仅拥有n = 1的基本情况,因为在计算n = 2时,您会发现它是n = 1和n = 0的总和? which would not be defined. 不会被定义。

Note: This is assuming you are 1-indexing the list of fib numbers ie 0 is the first 1 the second then 1 the third etc. 注意:这是假设您要对fib编号列表进行1索引编制,即0是第一个,第二个是1,然后是第三个,依此类推。

public class Fibonacci
{
    public static int fibonacci(int n)
    {
        if (n == 0)
            return 0;
        else if (n == 1)
            return 1;
        else
            return fibonacci(n - 1) + fibonacci(n - 2);
    }

    public static void main(String[] args)
    {
        System.out.println(fibonacci(11));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编写斐波那契数列的方法 - writing a method for fibonacci sequence 编写一个方法 isAbleToFly(),它不接受任何参数并返回 true 或 false? - Writing a method isAbleToFly(), which takes no argument and returns true or false? Fibonacci方法按顺序返回第n个数字 - Fibonacci method return nth number in sequence 制作一个返回数组中一位数的方法 - make a method which returns the number of one digit numbers in an array 应该使用什么名称的方法来返回嵌套对象的数量? - What name should have method which returns number of nested objects? 参数传递数量与映射传递数量相比,编写方法时哪个更好? - Passing number of parameters vs passing map, which is better while writing a method? 返回是否可以使用指定的字母计数生成指定单词的方法 - Method that returns whether a specified word can be made with specified letter counts 我生成了一个斐波那契数列,但现在我想在每个数字上提及哪个数字是奇数或偶数? - I have generated a fibonacci Series but now i want to mention on each number that which number is odd or even? 编写一个从搜索中返回布尔值的 java 方法 - writing a java method that returns a boolean from a search 斐波纳契数是负数 - Fibonacci number is negative
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM