向后斐波那契数列
这是代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这里没有 Java,但是斐波那契数字有一个明确的封闭形式:
其中
GoldenRatio = (1 + Sqrt[5])/2
所以你可以这样做:
输出:
编辑
作为旁注 黄金比例是自然、科学和艺术中常见的奇妙数字之一。
您可能会发现从海贝壳到帕台农神庙的黄金比例。
No Java here, but Fibonacci numbers have an explicit closed form:
Where
GoldenRatio = (1 + Sqrt[5])/2
So you can do:
Output:
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.
您可以边做边将它们插入到数组中,然后反转数组并将它们打印出来?
效率不高,但很容易做到。
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.
有两种可能性:
r[i]=r[i-2]-r[ 来即时生成并打印反向序列
。r
i-1]There are two possibilities:
r
on the fly by noting thatr[i]=r[i-2]-r[i-1]
.是的..就像其他人说的那样..我将存储在集合中,然后排序并打印
我刚刚修改了您的示例...运行它并看看这是否是您期望的行为。
}
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.
}
一种选择是将输出存储在数组中,然后向后遍历数组。
One option would be to store the outputs in an array as you go and then traverse the array backwards.
由于斐波那契数列的性质,您可以将所有元素存储到一个数据结构中,然后向后打印它们,因为每个值(第一个和第二个除外)取决于前两个值的总和。
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.
我也只是正常地运行序列(即不是反向)并将结果存储在集合中(可能是 ArrayList)。但是不需要在列表之后排序,甚至不需要以相反的顺序遍历列表,您可以在使用时将序列中的每个新“条目”添加到列表中的位置 0 中:
这将确保列表以相反的顺序存储序列。
这只是另一种可能的解决方案。
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:
This will ensure that the list stores the sequence in reverse order.
That is just another possible solution.