文字繞圈圈
輸入一個字串,請找出可以將這個字串填入的最小正方形,而填入的方式是由正方形的左上角開始,順時鐘方向,由外向內填入。
輸入說明:
每組輸入包含一列字串,字串內的字元都是英數字,長度小於100。
輸出說明:
對於每組測試資料,輸出填入最小正方形後的結果,每組測試資料以一列空白隔開。
輸入範例:
0123456789abcde
1111111111111111222222223
輸出範例:
0123
bcd4
a e5
9876
11111
12221
12321
12221
11111
Dim arr(10, 10) As String
回覆刪除Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Line Input #1, x
rn = Len(x)
n = CInt(Len(x) ^ 0.5 + 0.4)
X1 = 1: Y1 = 1: X2 = n: Y2 = n - 1: k1 = 1
rm = 1
Do Until rm > rn
step = Mid(x, rm, 1)
If X1 <= n Then
arr(X1, Y1) = step
X1 = X1 + 1
Else
If Y1 < n Then
arr(X1 - 1, Y1 + 1) = step
Y1 = Y1 + 1
Else
If X2 > k1 Then
arr(X2 - 1, Y1) = step
X2 = X2 - 1
Else
If Y2 > k1 Then
arr(X2, Y2) = step
Y2 = Y2 - 1
Else
k1 = k1 + 1
X1 = k1: Y1 = k1: X2 = n - 1: Y2 = n - 2: n = n - 1
rm = rm - 1
End If
End If
End If
End If
rm = rm + 1
Loop
For i = 1 To 10
For j = 1 To 10
If arr(j, i) = "" Then
Print #2, " ";
Else
Print #2, arr(j, i);
End If
Next j
Print #2,
Next i
Close #2
Close #1
End
End Sub
輸入(拿前面數讀題目
400805010200009080608003490005020600001070900007080300090100002080400009030602007
輸出
400805010
206000012
090100000
509030070
000076200
000020090
030040809
908070000
430080608
Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Dim ansP(10, 10) As String
While Not EOF(1)
Line Input #1, ques
If Len(ques) > 100 Then Print "輸入錯誤!!": GoTo NextLine
For i = 1 To 10
If i ^ 2 >= Len(ques) Then A = i: B = i: GoTo GoingON
Next i
GoingON:
If A ^ 2 > Len(ques) Then
coun = A ^ 2 - Len(ques)
For i = 1 To coun
ques = ques & " "
Next i
End If
f = 1
e = 2
k = 1
Do
For i = f To A
ansP(i, f) = Mid(ques, k, 1)
k = k + 1
Next i
For i = e To A
ansP(A, i) = Mid(ques, k, 1)
k = k + 1
Next i
For i = A - 1 To f Step -1
ansP(i, A) = Mid(ques, k, 1)
k = k + 1
Next i
For i = A - 1 To e Step -1
ansP(f, i) = Mid(ques, k, 1)
k = k + 1
Next i
A = A - 1
e = e + 1
f = f + 1
Loop Until A = 0
NextLine:
For i = 1 To B
For j = 1 To B
Print #2, ansP(j, i);
Next j
Print #2,
Next i
Print #2,
Wend
Close #2
Close #1
End
End Sub
輸入:
0123456789abcde
1111111111111111222222223
123456789456789123789123456231564897564897231897231564312645978645978312978312645
asdfasdfasdfas
輸出:
0123
bcd4
a e5
9876
11111
12221
12321
12221
11111
123456789
648975644
512645985
132978796
341453877
263621628
658795439
513279811
432198732
asdf
fasa
d s
safd
阿揚好,
回覆刪除1.所謂的巢狀if,有兩種形式。你的程式應該使用
if then
elseif
elseif
esle
end if
這樣的方式,比較不容易看錯。(只有一個end if)
2.程式ok。
小白好,
1.程式ok,雖然轉圈圈轉得很容易眼花。
2.程式一開始決定a,b的地方,稍嫌浪費時間了些,還是該用數學方式去計算,會好些。反正就是開根號出來的數字,如果是整數,就直接用,如果不是整數就加1。
熊掌 from Bankok
Dim ans(10, 10) As String
回覆刪除Dim a As String
Dim b(100) As String
Dim d 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 While Not EOF(1)
Line Input #1, a
c = Len(a)
d = Int(c ^ 0.5 + 0.4)
For i = 1 To c
b(i) = Left(a, 1)
a = Right(a, Len(a) - 1)
Next i
j = 1: k = 0: l = 1
For i = 1 To c
BACK:
Select Case l
Case 1
If k + 1 > d Or ans(j, k + 1) <> "" Then l = (l + 1) Mod 4: GoTo BACK
k = k + 1
ans(j, k) = b(i)
Case 2
If j + 1 > d Or ans(j + 1, k) <> "" Then l = (l + 1) Mod 4: GoTo BACK
j = j + 1
ans(j, k) = b(i)
Case 3
If j - 1 <= 0 Or ans(j - 1, k) <> "" Then l = (l + 1) Mod 4: GoTo BACK
j = j - 1
ans(j, k) = b(i)
Case 0
If k - 1 <= 0 Or ans(j, k - 1) <> "" Then l = (l + 1) Mod 4: GoTo BACK
k = k - 1
ans(j, k) = b(i)
End Select
Next i
For i = 1 To d
For j = 1 To d
Print #2, ans(i, j);
ans(i, j) = ""
Next j
Print #2,
Next i
Loop
Close #2
Close #1
End
End Sub
高仔好,
回覆刪除歡迎回來,這題OK,
只是個小地方,
Print #2, ans(i, j);
ans(i, j) = ""
後面這行是多的吧?