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

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

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

#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() 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...

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

private sub command1_click() ’ dim n as integer '定义一个整型变量n for n=1 to 10 '让n从1到10进行循环 sum=sum+fib(n)/fib(n+1) '对Sum变量进行求和运算,每循环一次sum加上 fib(n)/fib(n+1) next n print "sum";sum '打印经过循环运算之后...

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() Dim a As String Dim i as long Dim n as long Dim sum as long a = InputBox("输入斐波那契数列...

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