2011年3月8日 星期二

排序

排序
給n 個數字,請把它們由大到小排序好。

輸入說明:
輸入檔含有多組測試資料,每組測試資料有兩行,第一行的數字n 為有幾個數字要排序,
第二行則有n 個整數n ≤ 1000,而每個要排序數字的範圍為[-10000, 10000]間的整數。
輸出說明:
輸出已排序好的數列,每個數字之間用一個空白隔開。
輸入範例:

5
1 2 3 4 5
5
-5 -4 -3 -2 -1

輸出範例:
5 4 3 2 1
-1 -2 -3 -4 -5

11 則留言:

  1. ---選擇排序---

    Private Sub Form_Load()

    Me.Hide
    Dim N As Byte
    Dim X(50) As Integer
    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, X(i)
    Next i

    For i = 1 To (N-1)
    For j = (i + 1) To N
    If X(i) < X(j) Then Call change(X(i), X(j))
    Next j
    Next i

    For i = 1 To N
    Print #2, X(i);
    Next i

    Print #2,

    For i = 1 To N
    X(i) = 0
    Next i

    Loop

    Close #2
    Close #1
    End
    End Sub

    Sub change(A, B)
    C = A
    A = B
    B = C

    End Sub

    ---氣泡排序---


    Private Sub Form_Load()

    Me.Hide
    Dim N As Byte
    Dim X(50) As Integer
    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, X(i)
    Next i

    For i = 1 To N
    For j = 1 To i
    If X(i) > X(j) Then Call change(X(i), X(j))
    Next j
    Next i

    For i = 1 To N
    Print #2, X(i);
    Next i

    Print #2,

    For i = 1 To N
    X(i) = 0
    Next i

    Loop

    Close #2
    Close #1
    End
    End Sub

    Sub change(A, B)
    C = A
    A = B
    B = C

    End Sub



    ---結果---

    5 4 3 2 1
    -1 -2 -3 -4 -5

    回覆刪除
  2. 我也正想要讓你們做做這題說。
    而且,我想要的是你們用兩種方法來做,
    一個是用正常的排序,不管你是用氣泡還是選擇排序。(我想,你們應該還沒有人可以做出「快速排序」(quick sort)吧。)
    另一個是,用listbox, 但是,請注意在listbox中的排序是"字串"式的,要如何讓它也排序數字的內容呢。

    回覆刪除
  3. 佑好,
    程式正確做出排序,但是,第二個氣泡排序,和氣泡排序的定義不合吧。
    還有,題目是你自己post上去的吧,你的n是多少呢? 你說有1000個呢,但是你的程式呢? 你只願意做50個?

    回覆刪除
  4. 熊掌好,

    好久沒有寫氣泡排序
    忘了他是如何去定義

    Private Sub Form_Load()

    Me.Hide
    Dim N As Byte
    Dim X(1000) As Integer
    Dim P As Boolean
    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
    P = True

    For i = 1 To N
    Input #1, X(i)
    If X(i) < -10000 Or X(i) > 10000 Then P = False
    Next i

    If P = True Then

    For i = N To 1 Step -1
    For j = 1 To i
    If X(i) > X(j) Then Call change(X(i), X(j))
    Next j
    Next i

    For i = 1 To N
    Print #2, X(i);
    Next i

    Print #2,

    For i = 1 To N
    X(i) = 0
    Next i

    End If

    Loop

    Close #2
    Close #1
    End
    End Sub

    Sub change(A, B)
    C = A
    A = B
    B = C

    End Sub

    list 需要判斷他是不是負數、0<X<10!!
    負數又要反過來~
    有待思考

    回覆刪除
  5. 佑好,
    1. 這也不是氣泡。
    2.比賽的題目,原則上以正確的輸入為限。錯誤的輸入,先不理它。
    IIf X(i) < -10000 Or X(i) > 10000 Then P = False
    這一行可以省略。

    回覆刪除
  6. *氣泡
    Private Sub Form_Load()
    Me.Hide
    Dim x(1000) As Integer

    Open App.Path & "\out.txt" For Output As #2
    Open App.Path & "\in.txt" For Input As #1
    Input #1, r
    Randomize
    For i = 1 To r
    Input #1, x(i)
    Next

    For k = 1 To r - 1
    For i = 1 To r - 1
    For j = i To i + 1
    If x(i) < x(j) Then Call cha(x(i), x(j))
    Next j
    Next i
    Next k

    For i = 1 To r
    a = a & x(i) & " "
    Next
    Print a
    Close #1
    Close #2
    End
    End Sub
    Sub cha(a, b)
    t = a
    a = b
    b = t
    End Sub
    --------------------
    *選擇
    Private Sub Form_Load()
    Me.Hide
    Dim x(1000) As Integer

    Open App.Path & "\out.txt" For Output As #2
    Open App.Path & "\in.txt" For Input As #1
    Input #1, r

    For i = 1 To r
    Input #1, x(i)
    Next

    For i = 1 To r
    For j = i To r
    If x(i) < x(j) Then Call cha(x(i), x(j))
    Next j
    a = a & x(i) & " "
    Next i
    Print #2, a

    Close #1
    Close #2
    End
    End Sub
    Sub cha(a, b)
    t = a
    a = b
    b = t
    End Sub
    -------------
    in.txt
    5
    5 8 6 2 4
    out.txt
    8 6 5 4 2

    回覆刪除
  7. Private Sub Form_Load()
    Me.Hide
    Dim getnum As String, k1 As Integer, k2 As Integer
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do While Not EOF(1)
    List1.Clear
    List2.Clear
    Input #1, times
    k1 = 0: k2 = 0

    For i = 1 To times
    Input #1, a

    If a >= 0 Then
    k1 = k1 + 1
    B = Len(a)
    List1.AddItem B & " * " & a
    Else
    tmp = Abs(a)
    B = Len(tmp)
    List2.AddItem B & " * " & a
    k2 = k2 + 1
    End If

    Next
    '--正數--
    For i = k1 To 0 Step -1
    C = List1.List(i)
    getnum = Left(C, 1)
    ans = Right(C, Val(getnum)): Print #2, ans & " ";
    Next
    '--負數--
    For i = 0 To k2
    C = List2.List(i)
    getnum = Val(Left(C, 1)) + 1
    ans = Right(C, Val(getnum)): Print #2, ans & " ";
    Next

    Print #2,:
    Loop

    Close
    Close
    End
    End Sub



    //---------

    用 list 的方法

    我用了兩個 一個處理正數 一個處理負數

    //-------- In
    10
    2 -21 2463 245 -25 -51 50 1025 0 -1
    5
    -25 -224 -2123 -2 -12
    //-------- Out
    2463 1025 245 50 2 0 -1 -21 -25 -51

    -2 -12 -25 -224 -2123

    回覆刪除
  8. 回頭去看看一年級計概課本上的「氣泡排序」吧。
    緣尉的氣泡也弄錯了。
    arro的list,也可以。累了點就是了。

    回覆刪除
  9. *list
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\out.txt" For Output As #2
    Open App.Path & "\in.txt" For Input As #1

    Input #1, r

    For i = 1 To r
    Input #1, n
    n = n + 50000
    List1.AddItem n
    Next

    For i = r To 1 Step -1
    Print #2, (List1.List(i - 1) - 50000);
    Next

    Close #1
    Close #2
    End
    End Sub

    回覆刪除
  10. *氣泡
    Private Sub Form_Load()
    'Me.Hide
    Dim x(1000) As Integer

    Open App.Path & "\out.txt" For Output As #2
    Open App.Path & "\in.txt" For Input As #1
    Input #1, r

    For i = 1 To r
    Input #1, x(i)
    Next

    For i = 1 To r
    For j = 1 To r - 1
    If x(j) < x(j + 1) Then Call cha(x(i), x(j))
    Next j
    Next i

    For i = 1 To r
    a = a & x(i) & " "
    Next
    Print #2, a
    Close #1
    Close #2
    'End
    End Sub
    Sub cha(a, b)
    t = a
    a = b
    b = t
    End Sub

    回覆刪除
  11. 緣尉好,
    兩個程式都正確。
    但是,一個小地方,你在自己檢查程式中的設置,要交卷前,要去掉。
    'end 'me.hide
    或是其它時候會用print出一些中途的資料的部分。

    回覆刪除