2012年4月30日 星期一

基礎排序

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

輸入說明:

  每組測試資料共有兩行,第一行的數字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

3 則留言:

  1. Private Sub Form_Load()
    Me.Hide
    Dim a() As String
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do
    Input #1, n
    If n = 0 Then Exit Do
    Line Input #1, x
    a = Split(x)
    For i = 0 To UBound(a)
    For j = 0 To UBound(a) - 1
    If Val(a(j)) > Val(a(j + 1)) Then
    b = a(j)
    a(j) = a(j + 1)
    a(j + 1) = b
    End If
    Next
    Next
    For i = 0 To UBound(a)
    Print #2, a(i) & " ";
    Next
    Print #2,
    Loop Until n = 0
    Close
    Close
    End
    End Sub

    回覆刪除
  2. 作者已經移除這則留言。

    回覆刪除
  3. Dim ra() As Integer
    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
    Input #1, n
    If Val(n) = 0 Then Exit Do
    ReDim ra(Val(n))
    For i = 1 To Val(n)
    Input #1, r
    ra(i) = Val(r)
    Next i
    For i = 2 To Val(n)
    If ra(i - 1) < ra(i) Then
    ra(i - 1) = maxn
    ra(i - 1) = ra(i)
    ra(i) = maxn
    End If
    Next i
    For i = val(n) To 1 step -1
    Print #2, ra(i);
    Next i
    Print #2,
    Loop
    Close #2
    Close #1
    End
    End Sub

    回覆刪除