VB中输出斐波那契数列的前20项

如题所述

第1个回答  2010-01-14
分别用递推法和递归法求斐波那契数列:
Option Explicit
Option Base 1
Dim a() As Integer, n As Integer
Private Function f2(m As Integer) As Integer
Dim i As Integer
If m = 1 Or m = 2 Then
f2 = 1
Else
If m > 2 Then
For i = 3 To m
f2 = f2(i - 1) + f2(i - 2)
Next
End If
End If
End Function

Function f(m As Integer) As Integer
Dim i As Integer
ReDim a(m)

a(1) = 1
a(2) = 1
If m > 2 Then
For i = 3 To m
a(i) = a(i - 2) + a(i - 1)
Next
End If
f = a(m)
End Function

Private Sub Command1_Click()
Dim i As Integer
n = Val(InputBox("请输入第几个月"))
If n = 1 Then
Print 1;
Else
If n > 1 Then
Print 1,
For i = 2 To n
Print f(i),
If i Mod 5 = 0 Then Print
Next
End If
End If
Print

End Sub

Private Sub Command2_Click()
Dim i As Integer
n = Val(InputBox("请输入第几个月"))
If n = 1 Then
Print 1;
Else
If n > 1 Then
Print 1,
For i = 2 To n
Print f(i),
If i Mod 5 = 0 Then Print
Next
End If
End If
Print
End Sub

Private Sub Form_Load()
Command1.Caption = "递推法"
Command2.Caption = "递归法"
End Sub
第2个回答  2010-01-14
界面加一个按钮,直接复制运行

Private Sub Command1_Click()
Dim a(1 To 20) As Integer
For i = 1 To 20
If i = 1 Or i = 2 Then
a(i) = 1
Else
a(i) = a(i - 1) + a(i - 2)
End If

Print i, a(i)
Next i
End Sub本回答被提问者和网友采纳
第3个回答  2010-01-15
打表法蠢蠢欲动
print "1,1,2,3,5,8,13,21,34,55,89,143..."你懂的

vb中的斐波那契数列 前20项取值总和问题谢谢了,大神帮忙啊
斐波那契数列前三项分别是0、1、1,你只加了第二项或第三项 所以才会少1 Dim a&(20) a(1) = 0: a(2) = 1 For i = 3 To 20 a(i) = a(i - 2) + a(i - 1) b = a(i) + b Next i For i = 1 To 20 Print Right(Space(7) & a(i), 8); If i Mod 10...

VB中输出斐波那契数列的前20项
分别用递推法和递归法求斐波那契数列:Option Explicit Option Base 1 Dim a() As Integer, n As Integer Private Function f2(m As Integer) As Integer Dim i As Integer If m = 1 Or m = 2 Then f2 = 1 Else If m > 2 Then For i = 3 To m f2 = f2(i - 1) + f2(i -...

vb中的斐波那契数列 前20项取值总和问题如题 谢谢了
斐波那契数列前三项分别是0、1、1,你只加了第二项或第三项 所以才会少1 Dim a&(20) a(1) = 0: a(2) = 1 For i = 3 To 20 a(i) = a(i - 2) + a(i - 1) b = a(i) + b Next i For i = 1 To 20 Print Right(Space(7) & a(i), 8); If i Mod 10 =...

用vb输出斐波那契数列前20项的所有偶数
Public Class Form1 Dim arraa%(19)Dim i%, j Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click arraa(0) = 1 arraa(1) = 1 arraa(2) = 1 Label2.Text = "1 1 1"For i = 3 To 19 arraa(i) = arraa(i...

急!!!如何用VB编斐波那契数列列出前20项?
简单的方法...先建一个文本框和按钮 然后全复制 Private Sub Command1_Click()Dim m, n, i, t As Single i = 0 m = 0 n = 1 Do While i <= (Val(Text1.Text))i = i + 1 t = m + n n = m m = t Print t Loop End Sub 要几位就有几位......

如何用VB编斐波那契数列列出前20项而且是用循环语句FOR?
Private Sub Command1_Click()Dim a() As Long Dim sum As Long Dim n As Integer Dim i As Integer n = Val(Text1.Text)ReDim a(1 To n) As Long If n = 1 Then sum = 1 Else a(1) = 1 a(2) = 1 sum = 2 For i = 3 To n Step 1 a(i) = a(i - 1) + a(...

VB编程。。输出斐波那契数列1,1,2,3,5,8,13第20个数 使用for...next语 ...
Dim fbn(19) As Integer '定义斐波那契数列变量数组,共20个 fbn(0) = 1 '给出前两个值 fbn(1) = 1 For i = 2 To 19 fbn(i) = fbn(i - 2) + fbn(i - 1)Next i Print fbn(19)Sum = fbn(19)参考资料:自己编写的,亲试有效!结果是6765 ...

...+3\/2+5\/3+8\/5+13\/8+21\/12……求出这个数列的前20项之和
也可直接在“C语言”中编制如下小程序:include "stdio.h"main(){ int i;float m=1,n=1,t,s=0;clrscr();for(i=1;i<=20;i++){ t=m+n;s=s+t\/n;m=n;n=t;} printf("s is %9.6f\\n",s);getche();} 【这样的写法,完全符合斐波那契数列的展开顺序。本程序在TC3.0调试通过...

如何用VBfor循环求斐波那契数列以及它们的和值
斐波那契数列,又称黄金分割数列,斐波纳契数列以如下被以递归的方法定义:F1=,F2=1,Fn=F(n-1)+F(n-2)(n=2...那么怎样用vb中的for循环来求出斐波那契数列以及它们的和值呢?我们接着往下看 首先打开vb6新建工程exe,加载一个文本框text1,并且将它的多行显示属性设置为真(true)如图 接下来...

c语言费波那契数列:计算并输出数列的前20项
第二个 for 循环是打印数列项两个循环彼此没有联系 追问 那如果第一个for嵌套第二个for,又会怎样呢? 追答 for2 就是 把数组 f[] 里 下表可以被5整除的项打印出来, 如果是嵌套在 for1 循环里面,就是for1 每执行一次,打印一次。但是如果不修改for2 ,肯定会有乱码输出,因为打印的f[i] 可能还没赋值 本...

相似回答