编写VB程序,求数列的前n项之和:s=2⼀1+3⼀2+5⼀3+8⼀5+13⼀8…

急急急!!
2024年11月29日 19:38
有4个网友回答
网友(1):

Private Sub Command1_Click()
Dim n As Integer
n = InputBox("输入N值")
Dim a1 As Double
Dim a2 As Double
Dim a As Double
Dim sum As Double
a1 = 1
a2 = 1
For i = 1 To n
t = a1 + a2
a1 = a2
a2 = t
a = a2 / a1
sum = sum + a
Next i
Print "数列前" & n & "项和为" & sum
End Sub

网友(2):

这是数学问题哦,所以编程要有一定的数学知识的,要找出通项,在用循环做.
规律:
分母的n+2项是前两项的和,分子的n项是分母的n+1项,
先设置第一项和第二项的值,第三项就可以用公式计算了.

网友(3):

建立两个文本框,text1&text2.一个按钮,command1.
text1为n值.
text2为要求的结果.
command1为计算功能

Private Sub Command1_Click()
Dim a As Integer, b As Integer, c As Integer, i As Integer, x As Integer, y As Integer, z As Integer, n As Integer, sum As Double
n = Text1.Text
a = 2
b = 3
x = 1
y = 2
Select Case n
Case Is = 1
sum = 2
Case Is = 2
sum = 3.5
Case Is > 2
sum = 3.5
For i = 3 To n
c = a + b
a = b
b = c
z = x + y
x = y
y = z
sum = sum + c / z
Next i
End Select
Text2.Text = sum
End Sub

Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
End Sub

网友(4):

三个text , 一个button

Private Sub Command1_Click()

Dim i As Integer
Dim n As Integer
Dim dRet As Double

Dim mn As Double
Dim kn As Double
Dim t As Double
Dim s As String

n = Text1.Text

mn = 2
kn = 1
dRet = 0

For i = 1 To n
dRet = dRet + mn / kn
s = s & " + " & mn & "/" & kn
t = mn
mn = mn + kn
kn = t
Next i

Text2.Text = s
Text3.Text = dRet
End Sub