2011年10月19日 星期三

97模擬 Problem 3 (基礎排序

給一堆數字, 把他們從小到大排序好。

輸入說明:

  每組測試資料共有兩行,第一行的數字n 為有幾個數字要排序,第二行則有n 個整數(n≤1000),其餘整數皆於-10000到10000 之間,測試資料中包含多組測試,當排序個數為0 時結束。

輸出說明:
  輸出已排序好的數列,每個數字之間請用一個空白隔開。

輸入範例:
5
5 4 3 2 1
5
-1 -2 -3 -4 -5
0

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

2 則留言:

  1. Dim x()
    Private Sub Form_Load()
    Me.Hide
    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
    If N = 0 Then Exit Do
    ReDim x(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
    T = x(I)
    x(I) = x(J)
    x(J) = T
    End If
    Next J
    Next I

    ans = ""
    For I = 1 To N
    ans = ans & x(I) & " "
    Next I

    Print #2, ans
    Loop
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  2. Dim s
    Private Sub Form_Load()
    Me.Hide
    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
    If n <> 0 Then
    Line Input #1, m
    s = Split(m)
    Call P
    End If
    Loop
    Close
    Close
    End
    End Sub

    Sub P()
    ans = ""

    For j = 0 To UBound(s)
    For i = 0 To UBound(s) - 1
    If Val(s(i)) > Val(s(i + 1)) Then
    tmp = s(i)
    s(i) = s(i + 1)
    s(i + 1) = tmp
    End If
    Next
    Next

    For i = 0 To UBound(s)
    ans = ans & " " & s(i)
    Next

    Print #2, ans
    End Sub

    回覆刪除