2012年11月5日 星期一

菱形數字方塊


是寫一程式讀取"in.txt"之數字印出菱形數字方塊,其形狀是由一群小於該數的數字包圍而成,輸出至"out.txt"‧例如:輸入5則輸出形狀為

    1
   121
  12321
 1234321
123454321
 1234321
  12321
   121
    1
輸入2則形狀為
 1
121
 1

3 則留言:

  1. Dim num As Integer
    Dim ok(999) As String

    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Input #1, num
    Close #1

    For i = 1 To num
    For ai = 1 To i
    ok(i) = ok(i) & ai
    Next

    If Not i - 1 = 0 Then
    For ii = i - 1 To 1 Step -1
    ok(i) = ok(i) & ii
    Next
    End If

    For spacee = 1 To num - i
    ok(i) = " " & ok(i)
    Next

    Open App.Path & "/out.txt" For Output As #1
    For okk = 1 To num
    Print #1, ok(okk)
    Next

    For okkk = num - 1 To 1 Step -1
    Print #1, ok(okkk)
    Next
    Close #1

    Next
    End Sub

    回覆刪除
  2. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, n
    For k = n - 1 To 0 Step -1
    Print #2, Space(Int(k * 3));
    For i = 1 To n - k
    Print #2, i;
    Next
    For j = n - k - 1 To 1 Step -1
    Print #2, j;
    Next
    Print #2,
    Next
    For k = 1 To n - 1 Step 1
    Print #2, Space(Int(k * 3));
    For i = 1 To n - k
    Print #2, i;
    Next
    For j = n - k - 1 To 1 Step -1
    Print #2, j;
    Next
    Print #2,
    Next
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  3. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, x
    s = 0
    For i = 1 To x
    s = s + 1
    Print #2, Space((x - s) * 3);
    For k = 1 To i
    Print #2, k;
    Next
    For j = i - 1 To 1 Step -1
    Print #2, j;
    Next
    Print #2,
    Next
    s = 0
    For i = x - 1 To 1 Step -1
    s = s + 1
    Print #2, Space(s * 3);
    For k = 1 To i
    Print #2, k;
    Next
    For j = i - 1 To 1 Step -1
    Print #2, j;
    Next
    Print #2,
    Next
    Close
    Close

    回覆刪除