2012年11月7日 星期三

基礎排序

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

輸入說明:

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

5 則留言:

  1. Dim mynum, num(), temp 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, mynum
    If mynum = 0 Then Exit Do
    ReDim num(mynum)
    For i = 1 To mynum
    Input #1, num(i)
    Next

    For ii = 1 To mynum
    For iii = ii + 1 To mynum
    If num(ii) > num(iii) Then
    temp = num(ii)
    num(ii) = num(iii)
    num(iii) = temp
    End If
    Next
    Next

    For iiii = 1 To mynum
    Print #2, num(iiii);
    Next
    Print #2,
    Loop
    Close #2
    Close #1


    End
    End Sub

    回覆刪除
  2. 哈囉~把輸出說明看好吧~程式有小錯但不嚴重~不過還是謹慎一點好:)

    回覆刪除
  3. Dim mynum, num(), temp 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, mynum
    If mynum = 0 Then Exit Do
    ReDim num(mynum)
    For i = 1 To mynum
    Input #1, num(i)
    Next

    For ii = 1 To mynum
    For iii = ii + 1 To mynum
    If num(ii) > num(iii) Then
    temp = num(ii)
    num(ii) = num(iii)
    num(iii) = temp
    End If
    Next
    Next

    For iiii = 1 To mynum
    Print #2, num(iiii) & " ";
    Next
    Print #2,
    Loop
    Close #2
    Close #1


    End
    End Sub

    回覆刪除
  4. Dim a()
    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, s
    ReDim a(s)
    For i = 1 To s
    Input #1, a(i)
    Next
    If s <> 0 Then
    For i = 1 To s
    For j = 1 To s - 1
    If a(j) > a(j + 1) Then
    x = a(j)
    a(j) = a(j + 1)
    a(j + 1) = x
    End If
    Next
    Next

    For i = 1 To s
    Print #2, a(i) & " ";
    Next
    ElseIf s = 0 Then Exit Do
    End If
    Print #2,
    Loop
    Close
    Close
    End
    End Sub

    回覆刪除
  5. 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 ' Until s = 0
    Input #1, s
    If s = 0 Then Exit Do
    'If s <> 0 Then
    Line Input #1, x
    x = Split(x)
    For k = 0 To UBound(x)
    For j = k + 1 To UBound(x)
    If Val(x(k)) > Val(x(j)) Then
    a = Val(x(k))
    x(k) = Val(x(j))
    x(j) = a
    End If
    Next
    Next
    For t = 0 To UBound(x)
    Print #2, x(t) & Space(1);
    Next
    Print #2, ""
    'Else
    'Exit Do
    'End If
    'Input #1, s
    Loop
    End
    Close #2
    Close #1
    End Sub

    回覆刪除