Re: [請益]用遞迴寫x^n和求x y的最大公約數

看板Visual_Basic作者 (哇!阿魯米)時間19年前 (2005/05/18 11:55), 編輯推噓1(101)
留言2則, 2人參與, 最新討論串4/4 (看更多)
※ 引述《neplayer (neplayer)》之銘言: : ※ 引述《fumizuki (小獅)》之銘言: : : Function Power(Base As Integer, Exponent As Integer) : : If Exponent = 0 Then : : Power = 1 : : Else : : Power = Base * Power(Base, Exponent - 1) : : End If : : End Function : : 這是你的,你沒有傳回值,結果當然是零啦。 : : 至於公因數嘛,實在沒辦法... : : 我學 vb 的時候鮮少見到遞迴的範例,所以不太會寫遞迴/.\ : : 想了半天想不出個流程來:~~ : 回原po : gcd的程式應該google一下就一整票吧 : 想一下輾轉相除法 : function gcd(a as integer, b as integer) : if b=0 : gcd=a : else : gcd=gcd(b,a mod b) : end Public a As Double, b As Double, r As Double ____________________________________________________________ Private Sub Command1_Click() b = Val(Text1.Text) Mod Val(Text2.Text) r = Val(Text2.Text) Call GCD(b, r) Label4.Caption = r End Sub ____________________________________________________________ Public Function GCD(Number1 As Integer, Number2 As Integer) If Number2 = 0 Then GCD = Number1 Else GCD = GCD(Number2, (Number1 Mod Number2)) End If End Function ____________________________________________________________ 我已經寫出來了 PO出來給大家看看 之前PO的那個老師說不能= =" 好像說要改成這樣子 下面是我剛剛弄好的次方遞迴運算 Public x As Integer, y As Integer ____________________________________________________________ Private Sub Command1_Click() x = Val(Text1.Text) y = Val(Text2.Text) Label4.Caption = Power(x, y) End Sub ____________________________________________________________ Public Function Power(Base As Integer, Exponent As Integer) If Exponent = 0 Then Power = 1 Else Power = Base * Power(Base, Exponent - 1) End If End Function ____________________________________________________________ 終於弄完了... 謝謝各位幫忙喔^^ -- FUCKFUCKFUCK FUCK FUCK FUCKFUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCKFUCK FUCKFUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCKFUCK FUCKFUCK FUCK FUCK -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.128.194.155

140.128.194.155 05/18, , 1F
忘記說..要怎麼傳回次方那個funtion的值呢@@"
140.128.194.155 05/18, 1F

61.222.155.162 05/18, , 2F
函數名稱 = 傳回值
61.222.155.162 05/18, 2F
謝謝囉~我在試試看~^^" ※ 編輯: walume 來自: 140.128.194.155 (05/18 12:26)
文章代碼(AID): #12YhokTl (Visual_Basic)
文章代碼(AID): #12YhokTl (Visual_Basic)