数组列表中的斐波那契数列
我正在寻找对我的问题的清晰解释(不是寻找代码),但是如果一些代码有助于解释自己,那么请这样做..谢谢:)
问题:
-using Java
-Main 类要求用户提供一个整数输入(斐波那契 N 项),然后继续按顺序计算所有斐波那契数,直到达到该项。
-所有内容都存储在整数类型的单个数组列表中。 (每个数字都被分解并存储在自己的索引中,因此可以说,它是自己的“元素”。)
例如,我的目标是这样:
“请输入 N 斐波那契项:”
10
现在,在内部,我已将 2 个基本情况存储在 arraylist 中,如下所示:
ArrayList: [1, 1]
现在,我尝试在用户输入后使我的 arraylist 如下所示:
[1 , 1, 2, 3, 5, 8, 1, 3, 2, 1, 3, 4, 5, 5]
(注意它如何在最后一项 55 处停止,还要注意双数字值如何分解为单独的元素.)
我分解数字没有问题,只是“计算”给我带来了困难。提前感谢您的任何建议
I am looking for a clear explanation to my question (NOT looking for code), but if a bit of code helps to explain yourself, then please do.. thank you :)
Question:
-using Java
-Main class asks user for an integer input (fibonacci N term), then proceeds to calculate all the fibonacci numbers, in order, until it reaches that term.
-everything is stored into a single arraylists, of type integer. (Each digit is broken up and stored in its own index, so it is its own "element", so to speak.)
For example, i am aiming for it to go something like this:
"Please enter an N fibonacci term:"
10
At this point now, internally, I have stored the 2 base cases in an arraylist, that look like this:
ArrayList: [1, 1]
Now, I am trying to make my arraylist look like this, after user input:
[1, 1, 2, 3, 5, 8, 1, 3, 2, 1, 3, 4, 5, 5]
(notice how it stopped at the last term, 55, and also notice how the double digit values are broken up into separate elements.)
I have no problem breaking up the digits, its just the "calculating" that is giving me a hard time.. thanks in advance for any advice
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我将使用
int
值生成斐波那契值,并使用这些结果来分解数字以添加到 ArrayList。I would generate Fibonacci values using
int
values and use these results to break up digits to add to the ArrayList.听起来您想要从 F1 开始遍历斐波那契数列,同时将数字作为整数附加到 ArrayList中。由于您想要以 10 为基数的数字,我认为如果您将中间斐波那契整数转换为字符串,然后将字符串中的每个字符作为字符数组逐步执行,那么这将是最容易阅读的。当您逐步执行它时,您可以通过从中减去字符“0”将每个数字转换回整数。然后,您可以将该数字的数字版本追加到 ArrayList中。最终结果看起来像这样:
这里的要点是,当您沿着斐波那契数列前进时,不要弄乱您的数值。相反,您可以缓存“b”的副本,然后在确定将进入 ArrayList的每个数字的值之前将其转换为字符串。
It sounds like you want to walk the Fibonacci sequence, starting with F1, while appending the digits as ints to an
ArrayList<int>
as you go. Since you want the digits in base-10, I think this would be easiest to read if you convert the intermediate Fibonacci integers to strings, then step through each char in the string as a character array. As you step through it, you can convert each digit back to an integer by subtracting the char '0' from it. You can then append the numerical version of that digit to theArrayList<int>
. The end result would look like something like this:The takeaway here is that you don't mess around with your numerical values as you're walking up the Fibonacci sequence. Instead, you cache off a copy of 'b' that you transform into a string before determining the value of each digit that will go into the
ArrayList<int>
.好吧,当我尝试复制 Arraylist 时,它出现了语法错误,而其他响应没有足够的代码来显示它实际会做什么。不管怎样,你的老师给你的作业有点浪费编程。下面的内容将为您提供斐波那契数列的值。 num 是您想要的斐波那契数列中的哪个斐波那契数。例如,如果您想要该系列中的第 6 个斐波那契数,您可以在调用它时输入 fibi(6)。
一种修改它以便获得字符串数组的方法,但现在我需要去吃午饭
Well When I tried to copy the Arraylist thing it came up with a syntax error, and the other response on this didn't have enough code to show what it actually would do. Either way though the assignment your teacher gave you is sorta of a waste of programming. Below is something that will give you the value for Fibonacci numbers. num is which Fibonacci number in the Fibonacci series you want. Example if you want the 6th fibonacci number in the series you would put fibi(6) for when you call it.
A way you modify it so that you can get your string array, but right now I need to go to lunch
下面是制作字符串数组的方法。和小于 46 的原因是因为这是公共数组初始化长度的长度,并且您无法在初始化之外更改长度。您只需更改其中每个部分的内容即可。实际上,对于 int 斐波那契数使用其值,如果您尝试使用高于第 46 个斐波那契数的任何值,它将超出 int 的最大值。如果您使用 long,如果您尝试使用高于第 92 个斐波那契数的值,它将超出 long 的最大值。
Below is the way to make the string array. Reason for the and less than 46 is because that is how long the public array initialized length is, and you can't change the length outside of the initialization. You can just change what each piece in it is. Also in actually for int Fibonacci numbers for using their value if you try to use anything above the 46th fibonacci number it will go outside the max value for an int. If you use long its if you try to use something above the 92nd fibonacci number it will go outside the max value for a long.
我认为这应该锻炼。
i think this should workout.
编写一个经典的(对于 Java,只是,非递归的)斐波那契函数。
好的,您不需要 fib(N),因此更改循环的测试,使其继续直到达到 >=N。
此外,您不想返回 int,而是返回 ArrayList。因此,在函数中创建一个,将其添加到循环中,然后返回它。
哦,你也想做那个奇怪的数字事情。不要改变 #3 的功能;相反,使用它来获取斐波那契数字的 ArrayList,然后对其进行迭代,将数字添加到新的 ArrayList。
替代 #4,如果 Nishant 是正确的:从 #3 返回的 ArrayList 的索引 2 开始循环并向后查看,就像
“当然,在这种情况下,您可以继续编辑原始斐波那契函数,并跳过
的生成>fibs
ArrayList。Write a classical (just, made nonrecursive, for Java) Fibonacci function.
OK, you don't want fib(N), so change the test of your loop so that it continues until >=N is reached.
Also, you don't want to return an int, but an ArrayList. So create one in your function, add to it in your loop, and return it.
Oh, you also want to do that weird digit thing. Don't change your function from #3; instead, use it to get an ArrayList of Fibonacci numbers, and then iterate over that, adding digits to a new ArrayList.
Alternate #4, if Nishant is correct: loop from index 2 of the ArrayList returned by #3 and look backwards, as in
'course, in this case you could continue to edit your original Fibonacci function, and skip the production of the
fibs
ArrayList.我不擅长解释。但我认为这段代码可以满足您的要求。
I am not good at explaining. But i think this code can meet your requirement.