科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件VB数学实例:巧用递归法解不定方程

VB数学实例:巧用递归法解不定方程

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

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

作者:northwolves 来源:soft6 2008年5月15日

关键字: 递归法 数字 VB vb.net Windows

  • 评论
  • 分享微博
  • 分享邮件
 多元一次方程往往采用循环求解。笔者在与网友们讨论一个问题(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

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章