向后斐波那契数列

发布于 2024-10-29 16:53:16 字数 662 浏览 9 评论 0原文

这是代码:

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.

我试图改变高低,但我无法使这个程序强制“向后”运行。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

迟到的我 2024-11-05 16:53:16

这里没有 Java,但是斐波那契数字有一个明确的封闭形式

f[n_] := N@(GoldenRatio^n - (1 - GoldenRatio)^n)/Sqrt[5];  

其中

GoldenRatio = (1 + Sqrt[5])/2

所以你可以这样做:

For[i = 10, i > 0, i--,
     Print[f[i]];
  ];  

输出:

55.
34.
21.
13.
8.
5.
3.
2.
1.
1.

编辑

作为旁注 黄金比例是自然、科学和艺术中常见的奇妙数字之一。

您可能会发现从海贝壳到帕台农神庙的黄金比例。

No Java here, but Fibonacci numbers have an explicit closed form:

f[n_] := N@(GoldenRatio^n - (1 - GoldenRatio)^n)/Sqrt[5];  

Where

GoldenRatio = (1 + Sqrt[5])/2

So you can do:

For[i = 10, i > 0, i--,
     Print[f[i]];
  ];  

Output:

55.
34.
21.
13.
8.
5.
3.
2.
1.
1.

Edit

As an aside note The Golden Ratio is one of those wonderful pervasive numbers that you'll find in nature, science and the arts.

You may find the golden ratio from Sea Shells to the Parthenon.

谈情不如逗狗 2024-11-05 16:53:16

您可以边做边将​​它们插入到数组中,然后反转数组并将它们打印出来?
效率不高,但很容易做到。

You could insert them into an array as you go along, then just reverse the array and print them out?
Not exactly efficient, but it is easy to do.

春风十里 2024-11-05 16:53:16
int high = 8;
int low = 5;
while (low > 0) {
  System.out.println(high);
  int temp = low;
  low = high - low;
  high = temp;
}
int high = 8;
int low = 5;
while (low > 0) {
  System.out.println(high);
  int temp = low;
  low = high - low;
  high = temp;
}
春风十里 2024-11-05 16:53:16

有两种可能性:

  1. 存储数字而不是打印它们,最后反向打印它们。
  2. 向前运行算法以发现最后两个数字,然后通过注意到 r[i]=r[i-2]-r[ 来即时生成并打印反向序列 r i-1]

There are two possibilities:

  1. Store the numbers instead of printing them and at the end print them out in reverse.
  2. Run the algorithm forward to discover the last two numbers, and then produce and print the reverse series r on the fly by noting that r[i]=r[i-2]-r[i-1].
我做我的改变 2024-11-05 16:53:16

是的..就像其他人说的那样..我将存储在集合中,然后排序并打印

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

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

}

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

}

ぃ双果 2024-11-05 16:53:16

一种选择是将输出存储在数组中,然后向后遍历数组。

One option would be to store the outputs in an array as you go and then traverse the array backwards.

雨的味道风的声音 2024-11-05 16:53:16

由于斐波那契数列的性质,您可以将所有元素存储到一个数据结构中,然后向后打印它们,因为每个值(第一个和第二个除外)取决于前两个值的总和。

You can store all of the elements into a data structure then print them out backwards because of the nature of the Fibonacci sequence, since the each value (except for the first and second) depends on the sum of the two previous values.

没企图 2024-11-05 16:53:16

我也只是正常地运行序列(即不是反向)并将结果存储在集合中(可能是 ArrayList)。但是不需要在列表之后排序,甚至不需要以相反的顺序遍历列表,您可以在使用时将序列中的每个新“条目”添加到列表中的位置 0 中:

list.add(0, i + ": " + high + jel);

这将确保列表以相反的顺序存储序列。

这只是另一种可能的解决方案。

I too would just run through the sequence normally (i.e. not in reverse) and store the results in a collection (probably an ArrayList). But no need to sort after or even traverse the list in reverse order, you could just add each new "entry" in the sequence into position 0 in the list as you go using:

list.add(0, i + ": " + high + jel);

This will ensure that the list stores the sequence in reverse order.

That is just another possible solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文