x+2y+3z=k k為常數 如何用VB求出可能解
x,y,z皆為大於等於零之整數....
如何用VB程式求出所有可能的解呢
2006-07-04 10:09:11 · 3 個解答 · 發問者 beaver 2 in 電腦與網際網路 ➔ 程式設計
x,y,z皆有可能大於k/6
所以下面第一位的解應該不太正確
而且這樣跑...假如k=10000就要跑不少時間
不好意思忘記加一個條件...
x+y+z=t t也是正整數的常數.....抱歉
2006-07-04 11:02:45 · update #1
Private Sub Command1_Click() Dim K%, L%, P# K = 100 'K值 L = 3 '變數個數 P = Fn(K, L) MsgBox "結果已輸出至 C:\Fn.txt" & vbCrLf & "共 " & P & " 筆資料!"End SubFunction Fn(ByVal N As Integer, ByVal U As Integer) As Double Dim I%, T%, F&, P#, B As Boolean, S$, X%(), M%() U = U - 1 For I = 0 To U ReDim Preserve X(I), M(I) X(I) = N \ (I + 1) M(I) = I + 1 Next F = FreeFile Open "C:\Fn.txt" For Output As #F Do I = 0: T = N Do T = T - M(I) * X(I) If T < 0 Then T = T + M(I) * X(I) If X(I) > 0 Then X(I) = X(I) - 1 Else For J = I To U - 1 X(J) = N \ M(J) Next X(I - 1) = X(I - 1) - 1 I = 0: T = N End If Else I = I + 1 End If Loop Until I = U If T Mod M(U) = 0 Then S = "": X(U) = T / M(U) For I = 0 To U S = S & M(I) & "×" & X(I) & " + " Next Print #F, Mid$(S, 3, Len(S) - 4); " = "; N P = P + 1 End If B = True For I = U - 1 To 0 Step -1 If X(I) > 0 Then B = False: X(I) = X(I) - 1 For J = I + 1 To U - 1 X(J) = N \ M(J) Next Exit For End If Next Loop Until B Close #F Fn = PEnd Function
2006-07-06 11:55:40 · answer #1 · answered by W.J.S. 7 · 0⤊ 0⤋
記得要先拉一個CommandButton和ListBoxPrivate Sub Command1_Click()Dim K As IntegerDim Deep As ByteReDim SQ(0) As IntegerK = 20 '輸入你的K值Deep = 3 '輸入你的變數有幾個List1.Visible = FalseCall Cal(K, SQ(), Deep)List1.Visible = TrueEnd SubPrivate Sub Cal(N As Integer, Str1() As Integer, Deep As Byte)Dim FirstNum As IntegerDim Ans As StringFirstNum = 0Do ReDim Preserve Str1(UBound(Str1()) + 1) Str1(UBound(Str1())) = FirstNum If SQtoVal(Str1()) > N Then ReDim Preserve Str1(UBound(Str1()) - 1): Exit Do If UBound(Str1()) = Deep Then If SQtoVal(Str1()) = N Then For i = 1 To UBound(Str1()) Ans = Ans & Str1(i) & String(4 * i, " ") Next i List1.AddItem Ans ReDim Preserve Str1(UBound(Str1()) - 1) Exit Do End If ElseIf UBound(Str1()) < Deep Then DoEvents Call Cal(N, Str1(), Deep) End If ReDim Preserve Str1(UBound(Str1()) - 1) FirstNum = FirstNum + 1LoopEnd SubPrivate Function SQtoVal(SQ() As Integer) As IntegerFor i = 1 To UBound(SQ()) SQtoVal = SQtoVal + SQ(i) * iNext iEnd Function
2006-07-05 08:00:40 · answer #2 · answered by ? 5 · 0⤊ 0⤋
'k最小值應該是6
for z=1 to k/6
for y=1 to k/6
for x=1 to k/6
if (x+2*y+3*z)=k then
print "x=" & x
print "y=" & y
print "z="&z
end if
next x
next y
next z
2006-07-04 10:45:00 · answer #3 · answered by W.B.G 6 · 0⤊ 0⤋