在java中,如何找到第n个斐波那契数?

qxgroojn  于 2023-09-29  发布在  Java
关注(0)|答案(8)|浏览(103)

确定斐波那契序列很容易计算:

int num = 0;
int num2 = 1;
int loop;
int fibonacci;
System.out.print(num2);
for (loop = 1; loop <= 10; loop ++)
{
    fibonacci = num + num2;
    num = num2;
    num2 = fibonacci;
    System.out.print(" " + fibonacci);
}

我的问题在于试图确定指定的 N的值。 如,如果我想找到序列中的第6个元素,即8,我如何找到那个数字,并且只有那个数字?

jtjikinw

jtjikinw1#

在你的代码中,num从第0个斐波纳契数开始,num1从第1个斐波纳契数开始。所以要找到第n个,你必须迭代步骤n次:

for (loop = 0; loop < n; loop ++)
{
    fibonacci = num + num2;
    num = num2;
    num2 = fibonacci;
}
System.out.print(num);

等你写完了再打印
当循环计数器loop的值为k时,num保存第k个斐波那契数,num2保存第(k+1)个斐波那契数。

7z5jn7bk

7z5jn7bk2#

为了找到第n位数,我们需要知道斐波那契数的长度。可以使用Java的Integer.toString(int)函数将int转换为string。使用该字符串,然后可以确定转换后的斐波那契数的长度。
编辑:删除代码B/c可能的hwk问题

im9ewurl

im9ewurl3#

int n=5;//position of the fibonacci number to find
int fibonacci=0,num=0,num2=1;
for(int loop=1;loop<n;loop++)
{
   fibonacci=num+num2;
   num=num2;
   num2=fibonacci;
}
System.out.println(num);
6mzjoqzu

6mzjoqzu4#

import java.util.Scanner;
public class fibonacci{
public static void main(String[]args){
    Scanner i=new Scanner(System.in);
    String n=System.getProperty("line.separator");

    int count=0,x=0,y=1,sum;

    System.out.println("Enter a number:  ");
    int n=i.nextInt();

    for(count=0;count<n;count++){
        System.out.print(" "+ x);
        sum=x+y;
        x=y;
        y=sum;
    }
  }
}
0lvr5msh

0lvr5msh5#

我希望我的回答能有所帮助。我试图用动态规划的方法来解决这个问题。你只需要跟踪两个元素,直到n之前的一个索引。当你的代码在那里时,答案将是元素n-1和n-2的总和。

public class Fibonacci {

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

/**
 * Calculate the Fibonacci of n with two int variables and one temp variable. This is using dynamic programming.
 * Time complexity of this approach is O(n). Space complexity of this approach is O(k).
 * 0,1,1,2,3,5,8,13,21,34,55,89,144,...
 *
 *
 * @param n The nth Fibonacci number
 * @return The nth Fibonacci
 */
public static int fib(int n) {
    if (n == 0 || n == 1) {
        return n;
    }
    // Element 1
    int element1 = 0;
    // Element 2
    int element2 = 1;
    // Move the 2 elements window till one index before the nth.
    for (int i = 2; i < n; i++) {
        // Move the 2 elements window forward.
        int temp = element2;
        element2 = element1 + element2;
        element1 = temp;
    }
    // Return the nth Fibonacci by summing Fibonacci n-1 and n-2
    return element1 + element2;
}

}

w51jfk4q

w51jfk4q6#

public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    
    int n = s.nextInt();
    int num = 1;
    int num2 = 1;
    int fibonacci;
 
    for (int i=1; i<n; i++) {
        fibonacci = num + num2;
        num = num2;
        num2 = fibonacci;
    }
    System.out.print(num);
}
qcuzuvrc

qcuzuvrc7#

我已经将answer code provided by @Daniel Fischer重新构造为一个Java方法,您可以轻松地将其合并到您的开发中:

public class Post13021152 {
    public static int findNthFibonacciNumber(int n) {
        int num = 0;
        int num2 = 1;
        int loop;
        int fibonacci;
        for (loop = 0; loop < n; loop ++) {
            fibonacci = num + num2;
            num = num2;
            num2 = fibonacci;
        }
        return num;
    }
}
ctrmrzij

ctrmrzij8#

import acm.program.*;

public class FibonacciToN extends ConsoleProgram {

    public void run() {

        println("This program will display a table of Fibonacci numbers up to value n.");
        int n = readInt("Enter an integer for value n: ");
        int result = sequence(n);  

    }

    private int sequence(int n) {

        int a = 0;
        int b = 1;

        while (a < n) {  
            println(a); 
            a = a + b;
            b = a - b;
            }

        return a;
    }
}

相关问题