果洛皆料电子有限公司

VB數(shù)學(xué)實例:巧用遞歸法解不定方程

  • 發(fā)布于:2024-01-17
  • 194 人圍觀

多元一次方程往往采用循環(huán)求解。筆者在與網(wǎng)友們討論一個問題(http://expert.csdn.net/Expert/topic/2607/2607772.XML?temp=.7494928)過程中,琢磨出一種算法,采 用遞歸進(jìn)行多元一次方程的求解。并將解分為整數(shù)解和 非負(fù)整數(shù)解兩種情況,請大家指教。

Private Sub Command1_Click() '演示求X1 X2 X3 X4 X5=10整數(shù)解
Text1.Text = ""
Dim answer As String
answer = GETRESULT(5, 10, True) '賦值
Dim temp
temp = Split(answer, VBCrLf)
For i = 0 To UBound(temp)
temp(i) = "解" & i 1 & ":" & vbTab & temp(i) ' add index
Next
answer = Join(temp, vbCrLf)
Text1.Text = "方程 X1 X2 X3 X4 X5=10 共有 " & UBound(temp) 1 & " 個整數(shù)解:" & vbCrLf & answer 'show all answer in textbox

End Sub
Private Sub Command2_Click() '演示求X1 X2 X3 X4 X5=10非負(fù)整數(shù)解
Text1.Text = ""
Dim answer As String

answer = GETRESULT(5, 10, False) '賦值
Dim temp
temp = Split(answer, vbCrLf)
For i = 0 To UBound(temp)
temp(i) = "解" & i 1 & ":" & vbTab & temp(i) 'add index
Next
answer = Join(temp, vbCrLf)


萬企互聯(lián)
標(biāo)簽: