Re: [請益]用遞迴寫x^n和求x y的最大公約數
※ 引述《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
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)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 4 之 4 篇):
Visual_Basic 近期熱門文章
PTT數位生活區 即時熱門文章