递归方法巧解不定方程

       多元一次方程往往采用循环求解。笔者在与网友们讨论一个问题(http://expert.csdn.net/Expert/topic/2607/2607772.xml?temp=.7494928)过程中,琢磨出一种算法,采用递归进行多元一次方程的求解。并将解分为整数解和 非负整数解两种情况,请大家指教。

 

 

Private Sub Command1_Click() '演示求X1+X2+X3+X4+X5=10整数解
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 & " 个整数解:" & vbCrLf & answer 'show all answer in textbox

End Sub
Private Sub Command2_Click() '演示求X1+X2+X3+X4+X5=10非负整数解
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)
Text1.Text = "方程  X1+X2+X3+X4+X5=10 共有 " & UBound(Split(answer, vbCrLf)) + 1 & " 个非零整数解:" & vbCrLf & answer 'show all answer in textbox

End Sub

Private Sub Command3_Click() '演示无解情况
Text1.Text = ""
Dim answer As String

answer = GETRESULT(5, 3, False)
Dim temp
temp = Split(answer, vbCrLf)
For i = 0 To UBound(temp)
temp(i) = "解" & i + 1 & ":" & vbTab & temp(i)
Next
answer = Join(temp, vbCrLf)
Text1.Text = "方程  X1+X2+X3+X4+X5=3 共有 " & UBound(Split(answer, vbCrLf)) + 1 & " 个非零整数解:" & vbCrLf & answer

End Sub

'求解函数
Function GETRESULT(ByVal n As Integer, ByVal SUM As Integer, Optional allowzero As Boolean = True) As String
Dim temp() As String, i As Long
If n = 2 Then '二元方程
If allowzero = True Then
ReDim temp(SUM)
For i = 0 To SUM ' allow zero
temp(i) = "X1=" & i & ",X2=" & SUM - i
Next
GETRESULT = Join(temp, vbCrLf)
Erase temp
Else
ReDim temp(1 To SUM - 1) 'forbid zero
For i = 1 To SUM - 1
temp(i) = "X1=" & i & ",X2=" & SUM - i
Next
GETRESULT = Join(temp, vbCrLf)
Erase temp
End If

 

End If
If n > 2 Then

If allowzero = True Then
ReDim temp(SUM)
For i = SUM To 0 Step -1 ' allow zero
temp(i) = Replace(GETRESULT(n - 1, i, True), vbCrLf, ",X" & n & "=" & SUM - i & vbCrLf) & ",X" & n & "=" & SUM - i
Next
GETRESULT = Join(temp, vbCrLf)
Erase temp
Else
If SUM < n Then MsgBox "无解!": Exit Function '无解情况
ReDim temp(1 To SUM - n + 1) 'not allow zero
For i = 1 To SUM - n + 1
temp(i) = Replace(GETRESULT(n - 1, SUM - i, False), vbCrLf, ",X" & n & "=" & i & vbCrLf) & ",X" & n & "=" & i '递归
Next

GETRESULT = Join(temp, vbCrLf)
Erase temp
End If
End If
End Function

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
三元不定方程组可用归的方法,具体步骤如下: 1. 将三元不定方程组化为两个二元不定方程组。 2. 出其中一个二元不定方程组。 3. 将第一步中出的结果代入另一个二元不定方程组中,得到一个一元不定方程。 4. 出第三步中的一元不定方程,得到一个变量的值。 5. 将第四步中得到的变量值代入第二步中出的二元不定方程组中,得到另一个变量的值。 6. 将第五步中得到的变量值代入第一步中未出的二元不定方程组中,得到第三个变量的值。 7. 得到三个变量的值,即为方程组的。 以下是归求三元不定方程组的示例代码: ```python def solve_3_equations(a, b, c, d, e, f): if a == 0 and b == 0 and c == 0: # 当 a,b,c 都为 0 时,方程 return None if a == 0 and b == 0: # 当 a,b 都为 0 时,方程组化为一个一元不定方程 if f % c == 0: return [0, 0, f//c] else: return None if a == 0 and c == 0: # 当 a,c 都为 0 时,方程组化为一个一元不定方程 if e % b == 0: return [0, e//b, 0] else: return None if b == 0 and c == 0: # 当 b,c 都为 0 时,方程组化为一个一元不定方程 if d % a == 0: return [d//a, 0, 0] else: return None if a == 0: # 当 a 为 0 时,方程组化为一个二元不定方程 y, z = solve_2_equations(b, c, e, f) if y is None: return None else: return [0, y, z] if b == 0: # 当 b 为 0 时,方程组化为一个二元不定方程 x, z = solve_2_equations(a, c, d, f) if x is None: return None else: return [x, 0, z] if c == 0: # 当 c 为 0 时,方程组化为一个二元不定方程 x, y = solve_2_equations(a, b, d, e) if x is None: return None else: return [x, y, 0] # 当 a,b,c 都不为 0 时,先出其中一个二元不定方程 x, y = solve_2_equations(a, b, d, e) if x is None: return None else: # 将 x 代入另一个二元不定方程中,得到一个一元不定方程 z = (f - c*y - b*x) // a return [x, y, z] ``` 其中 `solve_2_equations` 是二元不定方程组的函数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值