2011年12月20日 星期二

菱形數字方塊

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

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


參照 -> http://chscvb.blogspot.com/2010/01/20100121.html

9 則留言:

  1. 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 x = 1 To n
    For i = x To n: Print #2, " ";: Next
    For i = 1 To x
    Print #2, i;
    Next
    For y = (x - 1) To 1 Step -1
    Print #2, y;
    Next
    Print #2,
    Next
    Print #2, ;
    For x = n - 1 To 1 Step -1
    For i = x To n: Print #2, " ";: Next
    For i = 1 To x
    Print #2, i;
    Next
    For y = (x - 1) To 1 Step -1
    Print #2, y;
    Next
    Print #2,
    Next

    Close
    Close
    End
    End Sub

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

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

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

    回覆刪除
  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

    Input #1, N

    LP = Space((N - 1) * 3)
    For X = 1 To N
    Print #2, LP;
    For i = 1 To X
    Print #2, i;
    Next

    For y = (X - 1) To 1 Step -1
    Print #2, y;
    Next
    If Len(LP) > 0 Then LP = Left(LP, Len(LP) - 3)
    Print #2,
    Next
    LP = " "

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

    For y = (X - 1) To 1 Step -1
    Print #2, y;
    Next
    Print #2,
    If Len(LP) < (N - 1) * 3 Then LP = LP & "  "
    Next

    Close
    Close
    End
    End Sub

    回覆刪除
  6. 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 x = 1 To n
    For i = x To n: Print #2, " ";: Next
    For i = 1 To x
    Print #2, i;
    Next
    For y = (x - 1) To 1 Step -1
    Print #2, y;
    Next
    Print #2,
    Next
    Print #2, ;
    For x = n - 1 To 1 Step -1
    For i = x To n: Print #2, " ";: Next
    For i = 1 To x
    Print #2, i;
    Next
    For y = (x - 1) To 1 Step -1
    Print #2, y;
    Next
    Print #2,
    Next

    Close
    Close
    End
    End Sub

    回覆刪除
  7. 小冰好,晟晟好,
    這是練習迴圈想法的題目,OK。
    雖然空白在網頁上會有影響,於是印出來的檔案,可能會排不整齊,但是,比賽並不會在這些地方做文章。
    解決的方法之一是用,space(n)來代替,但是,還好了。這題就先這樣子了。
    再來還很久,加油啦。
    累積一題一題的解題,就是功力。

    回覆刪除
  8. 把程式碼改過了一次,也將數字之間空格的問題改正
    也把空格改成space(n)的函數。

    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 x = 1 To n
    For i = x To n: Print #2, Space(1);: Next
    For i = 1 To x
    Print #2, i & "";
    Next
    For y = (x - 1) To 1 Step -1
    Print #2, y & "";
    Next
    Print #2,
    Next
    Print #2, ;
    For x = n - 1 To 1 Step -1
    For i = x To n: Print #2, Space(1);: Next
    For i = 1 To x
    Print #2, i & "";
    Next
    For y = (x - 1) To 1 Step -1
    Print #2, y & "";
    Next
    Print #2,
    Next

    Close
    Close
    End
    End Sub

    回覆刪除
  9. 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

    回覆刪除