private sub form_load() open app.path & "\in.txt" for input as #1 open app.path & "\out.txt" for output as #2 input #1,a for i = a-1 to 1 step -1 call mysub(i,a-i) next i close #1 close #2 end sub
private sub mysub(by val x, by val y) 'y是要分解的數,x是分解的最大數,就是將y都分成小於或等於x的數 if x>= y then print #2,x;y if y <> 1 then print #2,x; for j = y-1 to 1 step -1 call mysub(j,y-j) next j end if end sub
Private Sub Command1_Click() x = InputBox("") Call check((x), "", x - 1) End Sub
Sub check(s, str1, p) For i = p To 1 Step -1 If s = 0 Then Print str1 Exit Sub ElseIf s - i >= 0 Then Call check(s - i, str1 & i, i) End If Next i End Sub
Private Sub Command1_Click() x = InputBox("") Call divi(x, x - 1, "") End Sub
Sub divi(ByVal a, ByVal b, ByRef c As String) If a = 0 Then Print c For i = b To 1 Step -1 If a - i >= 0 Then Call divi(a - i, i, c & i) Next i End Sub
Private Sub Command1_Click() Open App.Path & "\out.txt" For Output As #1 Open App.Path & "\in.txt" For Input As #2 Input #2, x Call divi(x, x - 1, "") Close #1 End Sub
Sub divi(ByVal a, ByVal b, ByVal c As String) If a = 0 Then Print #1, c For i = b To 1 Step -1 If a - i >= 0 Then Call divi(a - i, i, c & " " & i) Next i End Sub
Private Sub Form_Load() Dim a As Integer, b As String, x As Integer, y As Integer a = InputBox("請輸入正整數") Open App.Path & "\t1.txt" For Output As #1 For i = a - 1 To 1 Step -1 Print #1, i, i Next i Close #1
For i = a - 1 To 1 Step -1 Open App.Path & "\t1.txt" For Input As #1 Open App.Path & "\t2.txt" For Output As #2 Do While Not EOF(1) Input #1, x, b y = a - x If y > x Then y = x If y > Val(Right(b, 2)) Then y = Val(Right(b, 2)) nomove = True For j = y To 1 Step -1 Print #2, x + j, b & " " & j nomove = False Next j If nomove = True Then Print #2, x, b Loop Close #1 Close #2
'交換兩個檔 t1.txt t2.txt Open App.Path & "\t2.txt" For Input As #1 Open App.Path & "\t1.txt" For Output As #2 Do While Not EOF(1) Line Input #1, b Print #2, b Loop Close #1 Close #2 Next i
'將檔案內容印出來 t1.txt Open App.Path & "\t1.txt" For Input As #1 Do While Not EOF(1) Input #1, x, b Print b Loop Close #1
3 3 1
回覆刪除2 2 2 1
2 2 1 1 1
不算嗎?
有遞迴的感覺呢。
回覆刪除好想來寫一寫哦。
哦,以為會寫得出來的,寫了大概二個小時,還是沒成功,改天再試吧。
回覆刪除下面這段,是最初寫的版本,後來改了幾次,還是錯很多,先放棄,但是,貼出來先記錄一下,改天有時間要想時,也可以先有個想法。
private sub form_load()
open app.path & "\in.txt" for input as #1
open app.path & "\out.txt" for output as #2
input #1,a
for i = a-1 to 1 step -1
call mysub(i,a-i)
next i
close #1
close #2
end sub
private sub mysub(by val x, by val y)
'y是要分解的數,x是分解的最大數,就是將y都分成小於或等於x的數
if x>= y then print #2,x;y
if y <> 1 then
print #2,x;
for j = y-1 to 1 step -1
call mysub(j,y-j)
next j
end if
end sub
來源http://tw.knowledge.yahoo.com/question/question?qid=1009112205457
回覆刪除Dim x As Integer
Private Sub Command1_Click()
x = InputBox("")
Call check((x), "", x - 1)
End Sub
Sub check(s, str1, p)
For i = p To 1 Step -1
If s = 0 Then
Print str1
Exit Sub
ElseIf s - i >= 0 Then
Call check(s - i, str1 & i, i)
End If
Next i
End Sub
參考了上面的程式,再去試試。
回覆刪除得到了我的版本,
Private Sub Command1_Click()
x = InputBox("")
Call divi(x, x - 1, "")
End Sub
Sub divi(ByVal a, ByVal b, ByRef c As String)
If a = 0 Then Print c
For i = b To 1 Step -1
If a - i >= 0 Then Call divi(a - i, i, c & i)
Next i
End Sub
再修改一下吧,因為那樣的呼叫,其實都是傳值呼叫。
回覆刪除每一次呼叫一次副程式,都會產生一次「副本」,而不影響上一次的值。
Private Sub Command1_Click()
Open App.Path & "\out.txt" For Output As #1
Open App.Path & "\in.txt" For Input As #2
Input #2, x
Call divi(x, x - 1, "")
Close #1
End Sub
Sub divi(ByVal a, ByVal b, ByVal c As String)
If a = 0 Then Print #1, c
For i = b To 1 Step -1
If a - i >= 0 Then Call divi(a - i, i, c & " " & i)
Next i
End Sub
divi(a,b,c)
回覆刪除a,是要被分解的數。
b,是最大不可以超過這個b數
c,是將分解的答案給連接在一起的字串。
今天用了檔案的方式,解了另一題"數學遊戲"中的 +_*/
回覆刪除想來,這題應該也可以用這樣的方式來解,而不用想不懂的"遞迴"。
試了試,果然可以。
程式如下:
Private Sub Form_Load()
Dim a As Integer, b As String, x As Integer, y As Integer
a = InputBox("請輸入正整數")
Open App.Path & "\t1.txt" For Output As #1
For i = a - 1 To 1 Step -1
Print #1, i, i
Next i
Close #1
For i = a - 1 To 1 Step -1
Open App.Path & "\t1.txt" For Input As #1
Open App.Path & "\t2.txt" For Output As #2
Do While Not EOF(1)
Input #1, x, b
y = a - x
If y > x Then y = x
If y > Val(Right(b, 2)) Then y = Val(Right(b, 2))
nomove = True
For j = y To 1 Step -1
Print #2, x + j, b & " " & j
nomove = False
Next j
If nomove = True Then Print #2, x, b
Loop
Close #1
Close #2
'交換兩個檔 t1.txt t2.txt
Open App.Path & "\t2.txt" For Input As #1
Open App.Path & "\t1.txt" For Output As #2
Do While Not EOF(1)
Line Input #1, b
Print #2, b
Loop
Close #1
Close #2
Next i
'將檔案內容印出來 t1.txt
Open App.Path & "\t1.txt" For Input As #1
Do While Not EOF(1)
Input #1, x, b
Print b
Loop
Close #1
End Sub