试图创建斐波纳契数列

时间:2013-09-27 16:24:26

标签: java variables printing int fibonacci

我不知道我做错了什么。我正在尝试打印序列的“fn”。我基于其他人的代码。有什么想法吗?

这是我的代码:

int num = n;
int fn = 1;
int f1 = 0;
int f2 = 1;

for (int i = 2; i < n; i++)
{
 fn = f1 + f2;
 f1 = f2;
 f2 = fn;

}

System.out.print( "fib( " + num + " ) is ");
System.out.println( fn );

4 个答案:

答案 0 :(得分:2)

我运行了你的代码以查看是否存在问题。以下是值2-10的各种程序输出:

fib( 2 ) is 1
fib( 3 ) is 1
fib( 4 ) is 2
fib( 5 ) is 3
fib( 6 ) is 5
fib( 7 ) is 8
fib( 8 ) is 13
fib( 9 ) is 21

输出接近正确,除非您每次都得到fib(n - 1)的结果。原因是您提前停止了for循环一次迭代。尝试使用<=修改条件:

for (int i = 2; i <= n; i++)

输出:

fib( 2 ) is 1
fib( 3 ) is 2
fib( 4 ) is 3
fib( 5 ) is 5
fib( 6 ) is 8
fib( 7 ) is 13
fib( 8 ) is 21
fib( 9 ) is 34

答案 1 :(得分:1)

static int fib(int n) {
    if (n == 0 || n == 1)
        return n;

    int f1 = 0;
    int f2 = 1;
    int fn = 0;

    for (int i = 2; i <= n; i++) {
        fn = f2 + f1;
        f1 = f2;
        f2 = fn;
    }

    return fn;
}

答案 2 :(得分:1)

通过添加序列的前两个成员来计算Fibonacci序列:

0 1         --the series starts like this.
0+1=1       so the series is now 
0 1 1       
  1+1=2     so the series continues...
0 1 1 2     and the next term is
    1+2=3   so we now have
0 1 1 2 3   and it continues as follows ...

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ...

你在考虑案例0吗?

e.g:

int n = Integer.parseInt(JOptionPane.showInputDialog("Input number:"));
BigInteger a = BigInteger.ONE;
BigInteger b = BigInteger.ZERO;
BigInteger c;
for (int i = 1; i <= n; i++) {
     c = a.add(b);
     a = b;
     b = c;
}
System.out.println(b);

f(12345) =

40080569507224047097051499321406575219228944077206339223411612103596633062182105
01082846030337166327710866380461665776652058343623273978850095367908925248215121
45173749742393351263429067658996935575930135482780507243981402150702461932551227
59043371327725570529753742801795702653627925205323772902863350712348310321084661
77747639361546735226645917360810397092944238656680469254927475839537583258506135
48914282578320544573036249175099094644435323970587790740267131607004023987409385
71616246095570779325753211277193270481671351919612883447072183609426501291804642
74491566540671950713589551040979737101509205368478774342567798867295556912132825
04703193401739340461924048504866698176130757935914248753973087073009601101912877
38363462892946760898398066418536337028673177171254258304136532864812454932387880
67583956523408611863340273923070910792571808356729897985240845346772523695859184
58720952520972332496025465803523315515681084895362126005441170936820059518262349
02245688875893867292085573973642391706512281634319217227130198100763607075137844
13630911872895221442278513821978071942563922949199120370194765824184512737679767
83751999133072126657949249799858935787018952232743400610036315564885371356712960
60896675518661262042586889262110662782513742538683165736882639824560614794427399
84983564433621701332349245316739393036680428782582821042127696252456803213440344
42698232414181912301904509531018692483863038992377680591406376081935756597411807
86483245242199312145954905504225330554559400911075373030206188102518205307407793
04945743042843818905340530656390842536418813634633111840242818352651038845390128
74542416238100890688593076189105555658375552988619203325356676814545718066196038
34568467183010292020985768291297156583889601129491834908879218410831868929923078
83556186380401867907243510736502105144291149055354110448887747138600413415933183
65792673354888566799196442017231870631867558530906286613228902689695061557951752
30968780656757329091090953539575814899437715863705011234765151784718812379079423
15727293456176196775555832070122531017013289717688278619224080643798912019728815
54890367344239218306050355964382953279316318309272212482218232309006973312977359
56255318460814457171307380228567550320922958131205725972936238278618310034396148
40908660575604740441898706339122005954780515737698899683422035125503026551174917
40823696686983281784153050366346823513213598551985596176977626982962058849363351
79430220670390757797006579383951159193074144107923417994348020653976756124427132
59233437520710389680021578899126949472040036377912710841909290583698015317878874
44598295425899927970

答案 3 :(得分:1)

你也可以尝试递归算法......

    if(n == 0)
        return 0;

    else if(n == 1)
        return 1;

    else
        return ( fibonacci(n - 1) + fibonacci(n - 2) );