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

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

#include int Fibonacci(int n) { if( n == 1 || n == 2) // 递归结束的条件,求前两项 return 1; else return Fibonacci(n-1)+Fibonacci(n-2); // 如果是求其它项,先要求出它前面两项,然后做和。 } int main() { int n; printf("please input...

程序: #include int fibonacci(int n){ if(n==0) return 0; if(n==1) return 1; else return fib(n-1)+fib(n-2); } void main(){ cout

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("输入斐波那契数列...

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

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

#include #define COL 10 //一行输出10个 long scan() { //输入求fibonacci函数的第N项 int n; printf("Input the N = "); scanf("%d",&n); return n; } long fibonacci(int n) { //fibonacci函数的递归函数 if (0==n||1==n) { //fibonacci函数...

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