当fibonacci序列> 1时,Iteral fibonacci java代码返回0。 2

时间:2015-04-09 04:57:28

标签: java fibonacci

下面的代码从Fibonacci 0到100进行交互。然而,在Fibonacci数达到2并且移到3之后,得到的数字为0.当它们应该是3,5,8等等时,我看到它应该工作的一切我不确定为什么。

但是你会注意到

else{
    return 0;
}

在代码的最后,我这样做是因为代码向我大吼,代码是返回long类型的值。如果有办法解决这个问题,请让我们知道谢谢你们。

 public static void runFibonacci () {
    for (int N = 0; N < 100; N++)
        StdOut.printf ("fibonacci(%2d)=%d\n", N, fibonacci (N));
}
public static long fibonacci (int N) {
    long low = 0;
    long tempLow = 0;
    long tempHi = 0;
    long hi = 0;
    if (N == 0) return 0;
    if (N == 1) {
        hi ++;
        return 1;
    }

    if (N == 2){
        low ++;
        return 1;
    }

    if (N > 2){
        tempLow = hi;
        tempHi = (low + hi);
        hi = tempHi;
        low = tempLow;
        return hi;
    }
    else{
        return 0;
    }
}

输出

fibonacci( 0)=0
fibonacci( 1)=1
fibonacci( 2)=1
fibonacci( 3)=0
fibonacci( 4)=0
fibonacci( 5)=0

预期输出

fibonacci( 0)=0
fibonacci( 1)=1
fibonacci( 2)=1
fibonacci( 3)=2
fibonacci( 4)=3
fibonacci( 5)=5
etc

3 个答案:

答案 0 :(得分:1)

正如@WannaBeCoder所说,最好的解决办法是让我的变量公开,你会看到我发布的代码顶部。如第一种方法调用的方法所述,每次都重置变量。这是有道理的。我学到了新的东西,我很感激

    public static long low = 0;
public static long tempLow = 0;
public static long tempHi = 0;
public static long hi = 0;

public static void runFibonacci () {
    for (int N = 0; N < 100; N++)
        StdOut.printf ("fibonacci(%2d)=%d\n", N, fibonacci (N));
}
public static long fibonacci (int N) {

    if (N == 0) return 0;
    if (N == 1) {
        hi ++;
        return 1;
    }

    if (N == 2){
        low ++;
        return 1;
    }

    if (N > 2){
        tempLow = hi;
        tempHi = (low + hi);
        hi = tempHi;
        low = tempLow;
        return hi;
    }
    else{
        return 0;
    }
}

<强>输出

fibonacci( 0)=0
fibonacci( 1)=1
fibonacci( 2)=1
fibonacci( 3)=2
fibonacci( 4)=3
fibonacci( 5)=5
fibonacci( 6)=8
fibonacci( 7)=13
fibonacci( 8)=21
fibonacci( 9)=34
fibonacci(10)=55
fibonacci(11)=89
fibonacci(12)=144
fibonacci(13)=233
fibonacci(14)=377
fibonacci(15)=610
fibonacci(16)=987
fibonacci(17)=1597
fibonacci(18)=2584
fibonacci(19)=4181
fibonacci(20)=6765
fibonacci(21)=10946
fibonacci(22)=17711
fibonacci(23)=28657
fibonacci(24)=46368
fibonacci(25)=75025
fibonacci(26)=121393
fibonacci(27)=196418
fibonacci(28)=317811
fibonacci(29)=514229
fibonacci(30)=832040
fibonacci(31)=1346269
fibonacci(32)=2178309
fibonacci(33)=3524578
fibonacci(34)=5702887
fibonacci(35)=9227465
fibonacci(36)=14930352
fibonacci(37)=24157817
fibonacci(38)=39088169
fibonacci(39)=63245986
fibonacci(40)=102334155
fibonacci(41)=165580141
fibonacci(42)=267914296
fibonacci(43)=433494437
fibonacci(44)=701408733
fibonacci(45)=1134903170
fibonacci(46)=1836311903
fibonacci(47)=2971215073
fibonacci(48)=4807526976
fibonacci(49)=7778742049
fibonacci(50)=12586269025
fibonacci(51)=20365011074
fibonacci(52)=32951280099
fibonacci(53)=53316291173
fibonacci(54)=86267571272
fibonacci(55)=139583862445
fibonacci(56)=225851433717
fibonacci(57)=365435296162
fibonacci(58)=591286729879
fibonacci(59)=956722026041
fibonacci(60)=1548008755920
fibonacci(61)=2504730781961
fibonacci(62)=4052739537881
fibonacci(63)=6557470319842
fibonacci(64)=10610209857723
fibonacci(65)=17167680177565
fibonacci(66)=27777890035288
fibonacci(67)=44945570212853
fibonacci(68)=72723460248141
fibonacci(69)=117669030460994
fibonacci(70)=190392490709135
fibonacci(71)=308061521170129
fibonacci(72)=498454011879264
fibonacci(73)=806515533049393
fibonacci(74)=1304969544928657
fibonacci(75)=2111485077978050
fibonacci(76)=3416454622906707
fibonacci(77)=5527939700884757
fibonacci(78)=8944394323791464
fibonacci(79)=14472334024676221
fibonacci(80)=23416728348467685
fibonacci(81)=37889062373143906
fibonacci(82)=61305790721611591
fibonacci(83)=99194853094755497
fibonacci(84)=160500643816367088
fibonacci(85)=259695496911122585
fibonacci(86)=420196140727489673
fibonacci(87)=679891637638612258
fibonacci(88)=1100087778366101931
fibonacci(89)=1779979416004714189
fibonacci(90)=2880067194370816120
fibonacci(91)=4660046610375530309
fibonacci(92)=7540113804746346429
fibonacci(93)=-6246583658587674878
fibonacci(94)=1293530146158671551
fibonacci(95)=-4953053512429003327
fibonacci(96)=-3659523366270331776
fibonacci(97)=-8612576878699335103
fibonacci(98)=6174643828739884737
fibonacci(99)=-2437933049959450366

答案 1 :(得分:0)

您可以使用递归获得斐波那契序列。您需要根据以下内容修改代码:

private int fibonacci(int n) {
        if (n == 0)
            return 0;
        else if (n == 1)
            return 1;
        else
            return fibonacci(n - 1) + fibonacci(n - 2);
    }

希望有所帮助。

答案 2 :(得分:0)

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone  // should be the class you are using.
{
    public static long low = 0;
    public static long tempLow = 0;
    public static long tempHi = 0;
    public static long hi = 0;  //made these global

    public static void main (String[] args) throws java.lang.Exception
    {
        //function from which you are calling runFibonacci.
        runFibonacci();
    }
    public static void runFibonacci () {
        for (int N = 0; N < 100; N++)
            System.out.println(fibonacci (N));
    }
    public static long fibonacci (int N) {

        long toReturn = 0;
        if (N == 0) return toReturn;
        if (N == 1) {
           hi ++;
           toReturn = 1;
        }

        if (N == 2){
            low ++;
            toReturn = 1;
        }

        if (N > 2){
            tempLow = hi;
            tempHi = (low + hi);
            hi = tempHi;
            low = tempLow;
            toReturn = hi;
        }
        return toReturn;
    }
}

对您的代码进行了一些小改动。这会奏效。每次调用函数时都会创建一组新的变量。