wzkl.net
当前位置:首页 >> vB题用递归 方法求数列前n项和 >>

vB题用递归 方法求数列前n项和

Form1上放一个文本框Text1, 一个按钮Command1。 在框中输入某个数字,例如10,代码会将列出f(1)至f(10)的值。 如果你只需要特定的第n项,比如第8项,就直接调用DiGui(8)即可。 代码: Private Sub Command1_Click() Dim n As Long n = CInt(Tex...

窗体中放入Text控件和命令按钮.取text中的数字作为N代码如下Private Function F(n As Long) As Long If n > 2 Then F = F(n - 1) + F(n - 2) Else F = 1 End If End FunctionPrivate Sub Command1_Click() Cls Dim I As Long I = Val(Text1.Text...

用递归方法计算斐波那契数列的第n项的代码如下: #include int Fibonacci(int n) { if( n == 1 || n == 2) // 递归结束的条件,求前两项 return 1; else return Fibonacci(n-1)+Fibonacci(n-2); // 如果是求其它项,先要求出它前面两项,然后做...

思路:使用递归主要有两点需要注意,一个是递归计算公式,二是递归跳出条件。 参考代码: #includeint fun(int n){if(n==0) return 0;//递归跳出条件 return n+fun(n-1);//递归计算公式 }int main(){int n;scanf("%d",&n); printf("%d\n",fun(n)...

int F(int n){if(n==0) //比较用==return 1; else if(n==1) //比较用==return 1;elsereturn (F(n-1)+F(n-2));}

添加一按钮command1,然后加入以下代码 Public Function feizi(n As Integer) If n = 1 Then feizi = 1 ElseIf n = 2 Then feizi = 2 Else feizi = feizi(n - 1) + feizi(n - 2) End If End Function Public Function feimu(n As Integer) If n ...

窗体中放入Text控件和命令按钮.取text中的数字作为N代码如下Private Function F(n As Long) As Long If n > 2 Then F = F(n - 1) + F(n - 2) Else F = 1 End If End FunctionPrivate Sub Command1_Click() Cls Dim I As Long I = Val(Text1.Text...

Dim a As Long, b As Long, c As Long Dim n As Long, i As Long, s As Double a = 2 b = 1 n = Val(InputBox("请输入n值")) For i = 1 To n s = s + a / b c = a a = a + b b = c Next MsgBox "和是" & s

#include fib(int n) {if(n==0) return(0); else if(n==1) return(1); else return(fib(n-1)+fib(n-2)); } main() {int n,s; scanf("%d",&n); s=fib(n); printf("%d\n",s); }

Private Sub Command1_Click() n = CInt(InputBox("N=")) y = fac(n) MsgBox n & "!=" & y End Sub Function fac(n) If n = 0 Or n = 1 Then fac = 1 Else fac = n * fac(n - 1) End Function

网站首页 | 网站地图
All rights reserved Powered by www.wzkl.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com