快捷搜索:  as  2018  FtCWSyGV  С˵  test  xxx  Ψһ  w3viyKQx

澳门银河总站手机版游戏网址_机器人论坛网



多元一次方程每每采纳轮回求解。笔者在与网友们评论争论一个问题(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)

Fo澳门银河总站手机版游戏网址r 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

a澳门银河总站手机版游戏网址nswer = GETRESULT(5, 3, False)

Dim temp

temp = Split(answer, vbCrLf)

For i = 0 To UBound(temp)

temp(i) = "解" & i + 1 & ":" & vbTab & temp(i)

Next

ans澳门银河总站手机版游戏网址wer = 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) = WordStr(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

您可能还会对下面的文章感兴趣: