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...

#include double RecursiveSum(int n, int max){if(n < max){return RecursiveSum(n+1, max) + (double)n/(2*n+1);}else{return (double)n/(2*n+1);}}void main(){int max;double result;scanf("%d", &max);result = RecursiveSum(1, max);print...

窗体中放入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项的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 Function Private Sub Command1_Click() Cls Dim N As Long, I As Long, sum As L...

添加一按钮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 ...

Private Sub Command1_Click() Print fbnqDt(20) Print fbnqDg(20) End Sub Rem 递归,自己调用自己,效率没递推的高 Private Function fbnqDg(ByVal n As Integer) As Double If n = 1 Or n = 2 Then fbnqDg = 1 ElseIf n > 2 Then fbnqDg = fb...

窗体中放入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...

设此所求的数列为:0,1,1,2,3,5,8,13,21,……(n=0,1,2,3,……) #include long Fibonacci(int n) { if(n==0||n==1) return (long)n; else return Fibonacci(n-1)+Fibonacci(n-2); } int main() { int n; scanf("%d",&n); printf("F(%d)=%ld\n",n,Fi...

使用递归进行操作时,关键是设置好递归条件。斐波那契数列的定义如下: f2 = f0 + f1 = 0 + 1 = 1,所以条件可以设置成当n==1或者n==2时,返回1。其它项则依次递归调用即可。 #include int f(int n){if(n==1 || n==2)return 1;elsereturn (f(n-2...

Private Sub Command1_Click() Dim n As Integer n = InputBox("请输入第几个月:") Print "Fib1("; n; ")="; fib1(n) Print "Fib2("; n; ")="; fib2(n)End SubFunction fib1(n As Integer) a = 1 b = 1 c = 1 For i = 3 To n c = a + b a = b ...

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