基礎vb,用函式形式程式設計 遞迴
1樓:網友
第1題:
新增乙個按鈕,完整**如下:
option explicit
private sub command1_click()print y(20)
end sub
private function y(byval n as integer) as single
if n = 1 then
y = 2 / 1
elsey = (n + 1) / n + y(n - 1)end if
end function
其餘的題目,原理相似。
vb編寫遞迴函式看不懂求解析。覺得難說明可以慢慢說,比較複雜的話我加分
2樓:網友
function fun(n as integer) as long
if n = 0 or n = 1 then
fun = 1
elsefun = n * fun(n - 1)
end if
end function
sub test()
msgbox fun(5)
end sub
從「功能」上說,fun函式的功能就是給乙個整數,返回乙個該數的階乘(一定要緊緊記住這個功能)
那麼階乘是怎麼實現的呢?
任何數n的階乘都是,n * n-1)!,有沒有例外呢?有!當n = 0 或者 n = 1的時候 n! = 1
好了,還記得剛才要牢記的那個「功能」麼?就好像現在我們已經寫好了這個"功能"函式fun(n)
前面已經說了 n = 1 or n = 0 時 自然返回 1
如果n 不等於1 就要用到n * n-1)! 而(n-1)!可以用fun(n-1)代替,因為他滿足這個函式的功能。
試著從"功能"上去理解函式,在想想吧,希望能幫到你。
3樓:聽不清啊
是這樣的:
if n=0 or n=1 then '如果n是1 或 0 的話,其階乘為1
fun=1else
fun=n*fun(n-1) '否則的話,n!=n*(n-1)!
end if
4樓:匿名使用者
第七空是1。第八控感覺有問題吖。因為求階乘是需要迴圈的,如:n的階乘,for i = 2 to n
fun = fun * i
next i
vb 遞迴函式題,急急急,**等
5樓:網友
這是菲坡那切數列。
function fib(byval n as long) as long
if n=1 or n=2 then
fib=1exit function
end if
fib=fib(n-1)+fib(n-2)end function
vb程式設計題,1.用函式呼叫的方式編寫求兩個自然數的最大公約數。2.計算5!+4!+3!+2!+1!,按照位址傳遞的方法
6樓:網友
第一題:
兩個textbox,乙個command。
option explicit
private function maxmodnum _(byval a as long, byval b as long) _
as long
dim c as long
c = 1do while c <>0
c = a mod b
a = bb = c
doevents
loopmaxmodnum = a
end function
private sub command1_click()if isnumeric( = false _or isnumeric( = false _then
msgbox "請輸入有效數字。"
exit sub
end if
msgbox "它們的最大公約數:" & maxmodnum(,end sub
第二題:乙個command。
option explicit
private sub myfunc _
byval n as integer, byref sum as long)
dim a, ts as long
sum = 0
do while n > 0
ts = 1
for a = 1 to n
ts = ts * a
doevents: next
sum = sum + ts
n = n - 1
doevents
loopend sub
private sub command1_click()dim sum as long
myfunc 5, sum
msgbox sum
end sub
遞迴函式程式設計計算1!+3!+5!+......n!(奇數)
7樓:大_匆
在我機子上執行了一下,沒有問題。
main()
printf("累加和s=%d", s);
int f(int a)
8樓:網友
#include
int f(int a)
int main()
printf("s=%d", s);
return 0;
上面是遞迴程式。
這個題目其實用dp做效率更高。
用VB程式設計畫圓
給你一段 參考吧。具體的引數,你可以自己修改一下。上張效果圖,在後面。private sub command1 click dim i as long me.cls for i 1 to 10 me.circle i 600 300,600 300,vbred next end sub privat...
c語言遞迴求階乘,c語言怎麼用遞迴呼叫函式的方法求n的階乘?
舉例 用遞迴方法求n include int main int n int y printf input a integer number scanf d n y fac n printf d d n n,y return 0 int fac int n int f if n 0 printf n ...
mod函式在vb中怎麼用,VB中Mod函式問題
mod是用來計算餘數的。前面是被除數,後面是除數,結果是餘數。語法 result number1modnumber2一般情況下,除數 被除數,很少用小數,結果一定是整數 商當然也是按照整數計算的 例如 5mod3 5除以3商1餘2,所以5mod3 2 23mod5.8 1 函式簡介 mod函式是一個...