斐波那契数列向后 [英] Fibonacci sequence backward

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

问题描述

这是代码:

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"步骤也要数字.

在此示例中,输出为:1,1,2,3,5,8,例如...但是我想按如下顺序显示它:例如...,8,5,3, 2 1 1

我试图更改上限和下限,但是我无法使该程序强制向后运行".

解决方案

是的.就像其他人所说的那样.我会存储在集合中,然后进行排序和打印

我刚刚修改了您的示例...运行它,看看这是否是您期望的行为.

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);
}

}

Here is the code:

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 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.

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.. 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天全站免登陆