UVA 11470題目
題意: N x N 方陣,最多10x10,算每一圈的數字和
vb版 解題參考,輸入 in.txt 輸出out.txt
https://uva.onlinejudge.org/external/114/11470.pdf
Public Class Form1
Dim a(10, 10) As Integer '最多 10x10
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.Hide()
FileOpen(1, "in.txt", OpenMode.Input)
FileOpen(3, "out.txt", OpenMode.Output)
For fn = 1 To 1
'If fn = 2 Then PrintLine(3)
Do While True
Dim n% = LineInput(fn)
If n = 0 Then Exit Do
For i = 0 To n - 1
Dim s = LineInput(fn)
Dim dat() = s.Split("
")
For j = 0 To n - 1
a(i, j) = dat(j)
Next
Next
PrintLine(3, fxx(n))
Loop
Next
End
End Sub
Dim dr() = {0, 1, 0, -1}
Dim dc() = {1, 0, -1, 0}
' d 0右 1下 2左 3上
Function fxx(ByVal n As Integer) As String
If (n = 1) Then Return a(0, 0)
fxx = ""
Dim m = (n + 1) \ 2 '共幾圈
Dim r, c, sum, k As Integer
For i = 0 To m - 1 '第 i 圈
sum = 0 : r = i : c = i
k = n - 1 - 2 * i '每個方向幾個數字
If k = 0 Then sum = a(r, c)
For d = 0 To 3 '四個方向
For j = 1 To k '每個方向幾個數字
r += dr(d) : c += dc(d) '下一個位置座標
sum += a(r, c)
Next j
Next d
If i > 0 Then fxx &= " "
fxx &= sum
Next i
End Function
End Class
0 意見:
張貼留言