6

请你帮我显示前 10 个斐波那契数字。我的代码显示以下结果:1、2、3、5、8、13、21、34、55,我还需要它来显示前两个斐波那契数(0 和 1)。我该怎么做?

Public Class Form1
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim a As Integer = 0
    Dim b As Integer = 1
    Dim fib As Integer = 0

    Do
      fib = a + b
      a = b
      b = fib
      Label1.Text = Label1.Text + fib.ToString & ControlChars.NewLine
    Loop While fib < 55
  End Sub
End Class

在专业编程中,您需要在哪里使用斐波那契数列?

4

9 回答 9

3

只需添加

Label1.Text = Label1.Text + a.ToString & ControlChars.NewLine
Label1.Text = Label1.Text + b.ToString & ControlChars.NewLine

之前Do ... while

有关与斐波那契数相关的应用,请参阅:斐波那契:应用

于 2011-04-03T18:57:52.360 回答
3

不要计算序列号中的下一个,然后将结果添加到输出中,而是按相反的顺序执行:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim a As Integer = 0
    Dim b As Integer = 1
    Dim fib As Integer 

    Do
        Label1.Text += a.ToString & ControlChars.NewLine
        fib = a + b
        a = b
        b = fib
    Loop While a <= 55

End Sub
于 2011-04-03T19:01:04.357 回答
1

就像您在代码中将前两个斐波那契数定义为 0 和 1 一样,您应该将它们放在开头的标签字符串中(即不在循环中)。您还应该对您计算的斐波那契数的数量使用循环条件,而不是依赖于知道第 10 个是什么。

我从来没有在工作中使用过斐波那契数,但是它们是一个很好的学习练习,使用天真的递归解决方案,一个带有查找表的简单迭代解决方案(如你的),使用黄金比例,矩阵形式......

于 2011-04-03T19:27:33.927 回答
1
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    Dim a As Integer = 0
    Dim b As Integer = 1
    Dim fib As Integer
    Dim userinput, i As Integer
    userinput = InputBox("how many")
    i = userinput
    ListView3.Items.Add(1)
    Do
        fib = a + b
        a = b
        b = fib
        ListView3.Items.Add(fib)
        i = i + 1
    Loop While fib < i
End Sub

结束类

于 2013-10-07T14:37:18.337 回答
0

试试这个代码:

Dim arr As New ArrayList()
    Console.Write("The Fibonacci Series is : ")
    For i As Integer = 0 To 10
        If i = 0 Or i = 1 Then
            arr.Add(i)
            Console.Write(arr(i).ToString() + ", ")               
        Else
            arr.Add(arr(i - 2) + arr(i - 1))
            If i = 10 Then
                Console.Write(arr(i).ToString())
            Else
                Console.Write(arr(i).ToString() + ", ")
            End If
        End If
    Next
    Console.Read()
于 2013-10-18T11:30:09.557 回答
0

Pretty Symple,只需使用一个按钮,您就可以根据需要生成任意数量的序列。

Sub fibonacci()

mycount = Application.CountA(Range("A:A"))

e = mycount - 1
fib = 0
fib = Cells(e, 1).Value + Cells(e + 1, 1).Value
Cells(mycount + 1, 1).Value = fib
mycount = mycount + 1

End Sub
于 2014-07-01T16:48:46.497 回答
0

子主()

    Dim previousfibo As Integer = 0
    Dim currentfibo As Integer = 1
    Dim nextfibo As Integer
    previousfibo = 0
    currentfibo = 1
    Console.WriteLine(previousfibo)
    Console.WriteLine(currentfibo)
    For I = 1 To 9

        nextfibo = previousfibo + currentfibo
        Console.WriteLine(nextfibo)
        previousfibo = currentfibo
        currentfibo = nextfibo
    Next I
    Console.ReadLine()




End Sub
于 2020-02-28T09:29:06.023 回答
-1
Dim a, b, c as integer

a=0

b=1

print a 

print b

while c<(n-c)

c=a+b

print c

a=b

b=c

wend

print "This is Fibonacci Series"

End Sub
于 2013-02-07T06:13:02.843 回答
-1
Dim n As integer 
n= inputBox("ENTER A NUMBER")
Dim a As integer 
Dim b As integer 
Dim I As integer 
a=0
b=1
Print b
for I= 1To n
c= a+b
Print c
a=b
b=c
Next
End Sub
于 2019-12-12T22:11:21.593 回答