Dim a(20) As Integer Private Sub Form_Load() Open App.Path & "\in.txt" For Input As #1 Open App.Path & "\out.txt" For Output As #2 While Not EOF(1) Input #1, n For i = 1 To n Input #1, a(i) Next i For i = 1 To n For j = 1 To n If a(i) > a(j) Then change a(i), a(j) End If Next j Next i For i = 1 To n Print #2, a(i); Next i Print #2, Wend Close #1 Close #2 End Sub Public Sub change(a, b) c = a a = b b = c End Sub
Private Sub Form_Load() Open App.Path & "\in.txt" For Input As #1 Open App.Path & "\out.txt" For Output As #2 Do Until EOF(1) Input #1, x Line Input #1, q w = Split(q) For i = 0 To UBound(w) - 1 For j = i + 1 To UBound(w) If Val(w(i)) < Val(w(j)) Then step = w(i) w(i) = w(j) w(j) = step End If Next j Next i For i = 0 To UBound(w) Print #2, w(i); " "; Next i Print Loop Close #2 Close #1 End Sub
Dim a(1000) As Integer Dim x(100) As Integer Private Sub Form_Load() Open App.Path & "\in.txt" For Input As #1 Open App.Path & "\out.txt" For Output As #2 k = 1 Do While Not EOF(1) Input #1, n For i = 1 To n Input #1, a(i) Next i For i = 1 To n For j = i To n If a(i) < a(j) Then ch a(i), a(j) Next j Next i For j = 1 To n Print a(j); Next j Print k = k + 1 Loop Close #2 Close #1 End Sub Public Sub ch(a, b) c = a a = b b = c End Sub
Dim V(1001) As Integer Private Sub Form_Load() Open App.Path & "/in.txt" For Input As #1 Open App.Path & "/out.txt" For Output As #2 Do While Not EOF(1) Input #1, n For i = 1 To n Input #1, V(i) Next i For i = 1 To n For j = i + 1 To n If V(i) > V(j) Then z = V(i): V(i) = V(j): V(j) = z Next j Next i For i = 1 To n Print #2, V(i); Next i Print #2, Loop Close #2 Close #1 End Sub
Dim a(20) As Integer
回覆刪除Private Sub Form_Load()
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
While Not EOF(1)
Input #1, n
For i = 1 To n
Input #1, a(i)
Next i
For i = 1 To n
For j = 1 To n
If a(i) > a(j) Then
change a(i), a(j)
End If
Next j
Next i
For i = 1 To n
Print #2, a(i);
Next i
Print #2,
Wend
Close #1
Close #2
End Sub
Public Sub change(a, b)
c = a
a = b
b = c
End Sub
阿瑋好,
回覆刪除這題OK, 但是,你們幾個在做這題之後,請用你們知道的三種排序法都做一次,就是氣泡、選擇、和插入排序法。
前兩種,你們知道怎麼回事,只是再花點兒時間。
插入排序的意思就是,一個一個地放入陣列中,放進去時,就是正確的位置。
例如輸入資料:34,45,2,10,55
排升冪。
第一次的結果陣列:34
第二次的結果陣列:34,45
第三次的,2是最小,那就將2插在第一個,將原先的第二、三個,都往後一個位置。得到:2,34,45
第四次的,10比第一個大,往後比。比第二個小,於是將二、三往後一個位置,將10放在第二個位置,得到:2,10,34,45
當然,第五次也類似。
試試吧。每個人。
Private Sub Form_Load()
回覆刪除Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Do Until EOF(1)
Input #1, x
Line Input #1, q
w = Split(q)
For i = 0 To UBound(w) - 1
For j = i + 1 To UBound(w)
If Val(w(i)) < Val(w(j)) Then
step = w(i)
w(i) = w(j)
w(j) = step
End If
Next j
Next i
For i = 0 To UBound(w)
Print #2, w(i); " ";
Next i
Print
Loop
Close #2
Close #1
End Sub
阿揚好,
回覆刪除程式OK.
其中 UBound(w)是不是等於 x-1呢?
對阿
回覆刪除Dim a(1000) As Integer
回覆刪除Dim x(100) As Integer
Private Sub Form_Load()
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
k = 1
Do While Not EOF(1)
Input #1, n
For i = 1 To n
Input #1, a(i)
Next i
For i = 1 To n
For j = i To n
If a(i) < a(j) Then ch a(i), a(j)
Next j
Next i
For j = 1 To n
Print a(j);
Next j
Print
k = k + 1
Loop
Close #2
Close #1
End Sub
Public Sub ch(a, b)
c = a
a = b
b = c
End Sub
小白好,
回覆刪除1.
ch a(i), a(j)
-->
call ch(a(i),a(j))
才是正常的副程式呼叫寫法。
2.這程式中的k 要做啥?
Dim V(1001) As Integer
回覆刪除Private Sub Form_Load()
Open App.Path & "/in.txt" For Input As #1
Open App.Path & "/out.txt" For Output As #2
Do While Not EOF(1)
Input #1, n
For i = 1 To n
Input #1, V(i)
Next i
For i = 1 To n
For j = i + 1 To n
If V(i) > V(j) Then z = V(i): V(i) = V(j): V(j) = z
Next j
Next i
For i = 1 To n
Print #2, V(i);
Next i
Print #2,
Loop
Close #2
Close #1
End Sub