递归方法巧解不定方程(二)
笔者在2004年曾写过一篇 递归方法巧解不定方程 。昨天在一位网友的启发下,对代码进行了重写,使其能够设置每个变量的取值范围。代码如下
Private Sub Command1_Click()
Dim min(1 To 5) As Integer, max(1 To 5) As Integer, RESULT
min(1) = 2
min(2) = 3
max(1) = 3
max(2) = 7
jiefangcheng 5, min, max, 15
End Sub
'求解函数
Sub GETRESULT(ByVal n As Integer, ByRef min() As Integer, ByRef max() As Integer, ByVal SUM As Integer, Optional ByRef resultcount As Long, Optional ByRef RESULT)
Dim temp(), c() As Long, i As Long, j As Long, k As Long, r As Long
If n = 1 Then '一元方程
If SUM >= min(1) And SUM <= max(1) Then
resultcount = 1
ReDim RESULT(1 To 1, 1 To 1)
RESULT(1, 1) = SUM
End If
End If
If n > 1 Then
resultcount = 0
ReDim temp(min(n) To max(n))
ReDim c(min(n) To max(n))
For i = min(n) To max(n)
GETRESULT n - 1, min, max, SUM - i, c(i), temp(i)
resultcount = resultcount + c(i)
Next
If resultcount > 0 Then
ReDim RESULT(1 To resultcount, 1 To n)
For i = min(n) To max(n)
If c(i) > 0 Then
For j = 1 To UBound(temp(i), 1)
r = r + 1
For k = 1 To n - 1
RESULT(r, k) = temp(i)(j, k)
Next
RESULT(r, n) = i
Next
End If
Next
Erase temp
End If
End If
End Sub
Sub jiefangcheng(ByVal n As Integer, ByRef min() As Integer, ByRef max() As Integer, ByVal SUM As Integer, Optional ByRef RESULT)
Dim x() As String, i As Integer, msg As String, resultcount As Long, laptime As Single
laptime = Timer
'默认解的范围为1-SUM
For i = 1 To n
If min(i) = 0 Then min(i) = 1
If max(i) = 0 Then max(i) = SUM
Next
GETRESULT n, min, max, SUM, resultcount, RESULT '赋值
ReDim x(1 To n)
For i = 1 To n
x(i) = "X" & i
Next
msg = "方程" & Join(x, "+") & "=" & SUM
For i = 1 To n
x(i) = "X" & i & "∈[" & min(i) & "," & max(i) & "]"
Next
msg = msg & "当" & Join(x, ",") & " 时" '关于方程的表达式和解的定义域
If resultcount > 0 Then
Debug.Print msg & "共有 " & resultcount & " 个解"
ReDim x(1 To resultcount)
For i = 1 To resultcount
x(i) = "解" & i & ":"
For j = 1 To n
x(i) = x(i) & "X" & j & "=" & RESULT(i, j) & vbTab
Next
Next
Debug.Print Join(x, vbCrLf)
Else
Debug.Print msg & "无解!"
End If
laptime = Timer - laptime
Debug.Print "总计用时" & IIf(laptime < 0, 0, Format(laptime, "0.000")) & "秒!"
End Sub
调试结果如下:
方程X1+X2+X3+X4+X5=15当X1∈[2,3],X2∈[3,7],X3∈[1,15],X4∈[1,15],X5∈[1,15] 时共有 190 个解
解1:X1=3 X2=7 X3=3 X4=1 X5=1
解2:X1=3 X2=6 X3=4 X4=1 X5=1
解3:X1=2 X2=7 X3=4 X4=1 X5=1
解4:X1=3 X2=5 X3=5 X4=1 X5=1
解5:X1=2 X2=6 X3=5 X4=1 X5=1
解6:X1=3 X2=4 X3=6 X4=1 X5=1
解7:X1=2 X2=5 X3=6 X4=1 X5=1
解8:X1=3 X2=3 X3=7 X4=1 X5=1
解9:X1=2 X2=4 X3=7 X4=1 X5=1
解10:X1=2 X2=3 X3=8 X4=1 X5=1
解11:X1=3 X2=7 X3=2 X4=2 X5=1
解12:X1=3 X2=6 X3=3 X4=2 X5=1
解13:X1=2 X2=7 X3=3 X4=2 X5=1
解14:X1=3 X2=5 X3=4 X4=2 X5=1
解15:X1=2 X2=6 X3=4 X4=2 X5=1
解16:X1=3 X2=4 X3=5 X4=2 X5=1
解17:X1=2 X2=5 X3=5 X4=2 X5=1
解18:X1=3 X2=3 X3=6 X4=2 X5=1
解19:X1=2 X2=4 X3=6 X4=2 X5=1
解20:X1=2 X2=3 X3=7 X4=2 X5=1
解21:X1=3 X2=7 X3=1 X4=3 X5=1
解22:X1=3 X2=6 X3=2 X4=3 X5=1
解23:X1=2 X2=7 X3=2 X4=3 X5=1
解24:X1=3 X2=5 X3=3 X4=3 X5=1
解25:X1=2 X2=6 X3=3 X4=3 X5=1
解26:X1=3 X2=4 X3=4 X4=3 X5=1
解27:X1=2 X2=5 X3=4 X4=3 X5=1
解28:X1=3 X2=3 X3=5 X4=3 X5=1
解29:X1=2 X2=4 X3=5 X4=3 X5=1
解30:X1=2 X2=3 X3=6 X4=3 X5=1
解31:X1=3 X2=6 X3=1 X4=4 X5=1
解32:X1=2 X2=7 X3=1 X4=4 X5=1
解33:X1=3 X2=5 X3=2 X4=4 X5=1
解34:X1=2 X2=6 X3=2 X4=4 X5=1
解35:X1=3 X2=4 X3=3 X4=4 X5=1
解36:X1=2 X2=5 X3=3 X4=4 X5=1
解37:X1=3 X2=3 X3=4 X4=4 X5=1
解38:X1=2 X2=4 X3=4 X4=4 X5=1
解39:X1=2 X2=3 X3=5 X4=4 X5=1
解40:X1=3 X2=5 X3=1 X4=5 X5=1
解41:X1=2 X2=6 X3=1 X4=5 X5=1
解42:X1=3 X2=4 X3=2 X4=5 X5=1
解43:X1=2 X2=5 X3=2 X4=5 X5=1
解44:X1=3 X2=3 X3=3 X4=5 X5=1
解45:X1=2 X2=4 X3=3 X4=5 X5=1
解46:X1=2 X2=3 X3=4 X4=5 X5=1
解47:X1=3 X2=4 X3=1 X4=6 X5=1
解48:X1=2 X2=5 X3=1 X4=6 X5=1
解49:X1=3 X2=3 X3=2 X4=6 X5=1
解50:X1=2 X2=4 X3=2 X4=6 X5=1
解51:X1=2 X2=3 X3=3 X4=6 X5=1
解52:X1=3 X2=3 X3=1 X4=7 X5=1
解53:X1=2 X2=4 X3=1 X4=7 X5=1
解54:X1=2 X2=3 X3=2 X4=7 X5=1
解55:X1=2 X2=3 X3=1 X4=8 X5=1
解56:X1=3 X2=7 X3=2 X4=1 X5=2
解57:X1=3 X2=6 X3=3 X4=1 X5=2
解58:X1=2 X2=7 X3=3 X4=1 X5=2
解59:X1=3 X2=5 X3=4 X4=1 X5=2
解60:X1=2 X2=6 X3=4 X4=1 X5=2
解61:X1=3 X2=4 X3=5 X4=1 X5=2
解62:X1=2 X2=5 X3=5 X4=1 X5=2
解63:X1=3 X2=3 X3=6 X4=1 X5=2
解64:X1=2 X2=4 X3=6 X4=1 X5=2
解65:X1=2 X2=3 X3=7 X4=1 X5=2
解66:X1=3 X2=7 X3=1 X4=2 X5=2
解67:X1=3 X2=6 X3=2 X4=2 X5=2
解68:X1=2 X2=7 X3=2 X4=2 X5=2
解69:X1=3 X2=5 X3=3 X4=2 X5=2
解70:X1=2 X2=6 X3=3 X4=2 X5=2
解71:X1=3 X2=4 X3=4 X4=2 X5=2
解72:X1=2 X2=5 X3=4 X4=2 X5=2
解73:X1=3 X2=3 X3=5 X4=2 X5=2
解74:X1=2 X2=4 X3=5 X4=2 X5=2
解75:X1=2 X2=3 X3=6 X4=2 X5=2
解76:X1=3 X2=6 X3=1 X4=3 X5=2
解77:X1=2 X2=7 X3=1 X4=3 X5=2
解78:X1=3 X2=5 X3=2 X4=3 X5=2
解79:X1=2 X2=6 X3=2 X4=3 X5=2
解80:X1=3 X2=4 X3=3 X4=3 X5=2
解81:X1=2 X2=5 X3=3 X4=3 X5=2
解82:X1=3 X2=3 X3=4 X4=3 X5=2
解83:X1=2 X2=4 X3=4 X4=3 X5=2
解84:X1=2 X2=3 X3=5 X4=3 X5=2
解85:X1=3 X2=5 X3=1 X4=4 X5=2
解86:X1=2 X2=6 X3=1 X4=4 X5=2
解87:X1=3 X2=4 X3=2 X4=4 X5=2
解88:X1=2 X2=5 X3=2 X4=4 X5=2
解89:X1=3 X2=3 X3=3 X4=4 X5=2
解90:X1=2 X2=4 X3=3 X4=4 X5=2
解91:X1=2 X2=3 X3=4 X4=4 X5=2
解92:X1=3 X2=4 X3=1 X4=5 X5=2
解93:X1=2 X2=5 X3=1 X4=5 X5=2
解94:X1=3 X2=3 X3=2 X4=5 X5=2
解95:X1=2 X2=4 X3=2 X4=5 X5=2
解96:X1=2 X2=3 X3=3 X4=5 X5=2
解97:X1=3 X2=3 X3=1 X4=6 X5=2
解98:X1=2 X2=4 X3=1 X4=6 X5=2
解99:X1=2 X2=3 X3=2 X4=6 X5=2
解100:X1=2 X2=3 X3=1 X4=7 X5=2
解101:X1=3 X2=7 X3=1 X4=1 X5=3
解102:X1=3 X2=6 X3=2 X4=1 X5=3
解103:X1=2 X2=7 X3=2 X4=1 X5=3
解104:X1=3 X2=5 X3=3 X4=1 X5=3
解105:X1=2 X2=6 X3=3 X4=1 X5=3
解106:X1=3 X2=4 X3=4 X4=1 X5=3
解107:X1=2 X2=5 X3=4 X4=1 X5=3
解108:X1=3 X2=3 X3=5 X4=1 X5=3
解109:X1=2 X2=4 X3=5 X4=1 X5=3
解110:X1=2 X2=3 X3=6 X4=1 X5=3
解111:X1=3 X2=6 X3=1 X4=2 X5=3
解112:X1=2 X2=7 X3=1 X4=2 X5=3
解113:X1=3 X2=5 X3=2 X4=2 X5=3
解114:X1=2 X2=6 X3=2 X4=2 X5=3
解115:X1=3 X2=4 X3=3 X4=2 X5=3
解116:X1=2 X2=5 X3=3 X4=2 X5=3
解117:X1=3 X2=3 X3=4 X4=2 X5=3
解118:X1=2 X2=4 X3=4 X4=2 X5=3
解119:X1=2 X2=3 X3=5 X4=2 X5=3
解120:X1=3 X2=5 X3=1 X4=3 X5=3
解121:X1=2 X2=6 X3=1 X4=3 X5=3
解122:X1=3 X2=4 X3=2 X4=3 X5=3
解123:X1=2 X2=5 X3=2 X4=3 X5=3
解124:X1=3 X2=3 X3=3 X4=3 X5=3
解125:X1=2 X2=4 X3=3 X4=3 X5=3
解126:X1=2 X2=3 X3=4 X4=3 X5=3
解127:X1=3 X2=4 X3=1 X4=4 X5=3
解128:X1=2 X2=5 X3=1 X4=4 X5=3
解129:X1=3 X2=3 X3=2 X4=4 X5=3
解130:X1=2 X2=4 X3=2 X4=4 X5=3
解131:X1=2 X2=3 X3=3 X4=4 X5=3
解132:X1=3 X2=3 X3=1 X4=5 X5=3
解133:X1=2 X2=4 X3=1 X4=5 X5=3
解134:X1=2 X2=3 X3=2 X4=5 X5=3
解135:X1=2 X2=3 X3=1 X4=6 X5=3
解136:X1=3 X2=6 X3=1 X4=1 X5=4
解137:X1=2 X2=7 X3=1 X4=1 X5=4
解138:X1=3 X2=5 X3=2 X4=1 X5=4
解139:X1=2 X2=6 X3=2 X4=1 X5=4
解140:X1=3 X2=4 X3=3 X4=1 X5=4
解141:X1=2 X2=5 X3=3 X4=1 X5=4
解142:X1=3 X2=3 X3=4 X4=1 X5=4
解143:X1=2 X2=4 X3=4 X4=1 X5=4
解144:X1=2 X2=3 X3=5 X4=1 X5=4
解145:X1=3 X2=5 X3=1 X4=2 X5=4
解146:X1=2 X2=6 X3=1 X4=2 X5=4
解147:X1=3 X2=4 X3=2 X4=2 X5=4
解148:X1=2 X2=5 X3=2 X4=2 X5=4
解149:X1=3 X2=3 X3=3 X4=2 X5=4
解150:X1=2 X2=4 X3=3 X4=2 X5=4
解151:X1=2 X2=3 X3=4 X4=2 X5=4
解152:X1=3 X2=4 X3=1 X4=3 X5=4
解153:X1=2 X2=5 X3=1 X4=3 X5=4
解154:X1=3 X2=3 X3=2 X4=3 X5=4
解155:X1=2 X2=4 X3=2 X4=3 X5=4
解156:X1=2 X2=3 X3=3 X4=3 X5=4
解157:X1=3 X2=3 X3=1 X4=4 X5=4
解158:X1=2 X2=4 X3=1 X4=4 X5=4
解159:X1=2 X2=3 X3=2 X4=4 X5=4
解160:X1=2 X2=3 X3=1 X4=5 X5=4
解161:X1=3 X2=5 X3=1 X4=1 X5=5
解162:X1=2 X2=6 X3=1 X4=1 X5=5
解163:X1=3 X2=4 X3=2 X4=1 X5=5
解164:X1=2 X2=5 X3=2 X4=1 X5=5
解165:X1=3 X2=3 X3=3 X4=1 X5=5
解166:X1=2 X2=4 X3=3 X4=1 X5=5
解167:X1=2 X2=3 X3=4 X4=1 X5=5
解168:X1=3 X2=4 X3=1 X4=2 X5=5
解169:X1=2 X2=5 X3=1 X4=2 X5=5
解170:X1=3 X2=3 X3=2 X4=2 X5=5
解171:X1=2 X2=4 X3=2 X4=2 X5=5
解172:X1=2 X2=3 X3=3 X4=2 X5=5
解173:X1=3 X2=3 X3=1 X4=3 X5=5
解174:X1=2 X2=4 X3=1 X4=3 X5=5
解175:X1=2 X2=3 X3=2 X4=3 X5=5
解176:X1=2 X2=3 X3=1 X4=4 X5=5
解177:X1=3 X2=4 X3=1 X4=1 X5=6
解178:X1=2 X2=5 X3=1 X4=1 X5=6
解179:X1=3 X2=3 X3=2 X4=1 X5=6
解180:X1=2 X2=4 X3=2 X4=1 X5=6
解181:X1=2 X2=3 X3=3 X4=1 X5=6
解182:X1=3 X2=3 X3=1 X4=2 X5=6
解183:X1=2 X2=4 X3=1 X4=2 X5=6
解184:X1=2 X2=3 X3=2 X4=2 X5=6
解185:X1=2 X2=3 X3=1 X4=3 X5=6
解186:X1=3 X2=3 X3=1 X4=1 X5=7
解187:X1=2 X2=4 X3=1 X4=1 X5=7
解188:X1=2 X2=3 X3=2 X4=1 X5=7
解189:X1=2 X2=3 X3=1 X4=2 X5=7
解190:X1=2 X2=3 X3=1 X4=1 X5=8
总计用时0.410秒!