斐波那契序列 [英] Fibonacci sequence

查看:150
本文介绍了斐波那契序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是代码:

class Fibonacci {
    static final int MIN_INDEX = 1;
    public static void main (String[] args){
        int high = 1;
        int low = 1;
        String jel;
        System.out.println("9: " + high);

    for (int i = 8; i >= MIN_INDEX; i--){
        if (high % 2 == 0)
            jel = " *";
        else 
            jel = " ";
        System.out.println(i + ": " + high + jel);
        high = low + high;
        low = high - low;


    }
}
}

我想制作这个程序,向后写输出数字。所以我想要的不仅是从最后一个到第一个的'i'步,而且数字也是。

I want to make this program, to write the output numbers backward. So I want that not only the ' i ' step from the last to the first, but the numbers too.

在这个例子中,输出为:1,1, 2,3,5,8,例如...但我希望在序列中显示它:例如......,8,5,3,2,1,1。

In this example, the output is: 1, 1, 2, 3, 5, 8 , eg... But I want to show it in the sequence looks like: eg... , 8, 5, 3, 2, 1, 1.

我试图改变高低,但我不能让这个程序强制向后运行。

I tried to change the high and low, but I can't make this program force to run "backward".

推荐答案

Yup ..就像其他人说...我会存储在一个集合中,然后排序和打印

Yup.. just like the other folks are saying.. i would store in a collections, then sort and print

我刚修改了你的例子...运行它,看看这是否是你期望的行为。

i just modified your example... run it and see if this is the behavior you expect.

class Fibonacci {
static final int MIN_INDEX = 1;

public static void main(String[] args) {
    int high = 1;
    int low = 1;
    String jel;
    List<String> numbers = new ArrayList<String>();
    numbers.add("9: " + high);

    for (int i = 8; i >= MIN_INDEX; i--) {
        if (high % 2 == 0) {
            jel = " *";
        }
        else {
            jel = " ";
        }
        numbers.add(i + ": " + high + jel);
        high = low + high;
        low = high - low;
    }

    Collections.sort(numbers);
    System.out.println(numbers);
}

}

这篇关于斐波那契序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆