2012年11月20日 星期二

文字繞圈圈


文字繞圈圈
輸入一個字串,請找出可以將這個字串填入的最小正方形,而填入的方式是由正方形的左上角開始,順時鐘方向,由外向內填入。
輸入說明:
 每組輸入包含一列字串,字串內的字元都是英數字,長度小於100。
輸出說明:
 對於每組測試資料,輸出填入最小正方形後的結果,每組測試資料以一列空白隔開。
輸入範例:
 0123456789abcde
1111111111111111222222223

輸出範例:
0123
bcd4
a e5
9876

11111
12221
12321
12221
11111

1 則留言:

  1. Dim lenth, x, y, lenthh, mycount, k As Integer
    Dim num(10, 10) As String
    Dim turn As Boolean
    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
    Line Input #1, strr
    For i = 2 To 20
    If Len(strr) = i ^ 2 Then lenthh = i: Exit For
    If Len(strr) < i ^ 2 Then
    lenthh = i
    For ai = 1 To i ^ 2 - Len(strr)
    strr = strr & " "
    Next
    Exit For
    End If
    Next
    x = 1: y = 1: turn = False: k = 1: mycount = 0
    For ii = 1 To lenthh ^ 2
    If num(x, y) = "" Then num(x, y) = Mid(strr, ii, 1)
    Call change
    If mycount = 2 Then k = -k: mycount = 0: Call change
    Next
    For ki = 1 To lenthh
    For kii = 1 To lenthh
    Print #2, num(ki, kii);: num(ki, kii) = ""
    Next
    Print #2,
    Next
    Print #2,
    Loop Until EOF(1)
    Close #2
    Close #1
    End
    End Sub
    Public Sub change()
    If (y + k > lenthh Or y + k = 0 Or num(x, y + k) <> "") And turn = False Then turn = Not turn: mycount = mycount + 1
    If turn = False And y + k <= lenthh And y + k > 0 Then y = y + k
    If (x + k > lenthh Or x + k = 0 Or num(x + k, y) <> "") And turn = True Then turn = Not turn: mycount = mycount + 1
    If turn = True And x + k <= lenthh And x + k > 0 Then x = x + k
    End Sub

    回覆刪除