|
改成顺时针、逆时针两个方向,矩阵大小设置为x行、y列。- Sub test1() 'by kagawa
- Dim i&, j&, i1&, j1&, i2&, j2&, k&, m&, r&, x&, y&, z&
- [a1].CurrentRegion = ""
- x = InputBox("行数x=")
- y = InputBox("列数y=")
- z = MsgBox("顺时针Yes/逆时针No", vbYesNo)
- ReDim a(1 To x, 1 To y)
- m = x * y
- i1 = 1: j1 = 1
- If z = vbYes Then
- i2 = 1: j2 = y: r = 2 '2-4-1-3
- Else
- i2 = x: j2 = 1: r = 4 '4-2-3-1
- End If
- Do
- For i = i1 To i2
- For j = j1 To j2
- k = k + 1
- If r Mod 2 Then
- If r = 1 Then
- If z = vbYes Then a(i, y - j) = k Else a(i, y - j + 1) = k
- Else 'r=3
- If z = vbYes Then a(x - i + 1, j) = k Else a(x - i, j) = k
- End If
- Else
- a(i, j) = k
- End If
- [a1].Resize(x, y) = a
- Next
- Next
- If k = m Then Exit Do
- If r = 1 Then
- If z = vbYes Then '2-4-1-3
- j2 = j1: i1 = x - i2 + 2: i2 = i2 - 1: r = 3
- Else '4-2-3-1-4
- j2 = j1: i2 = x - i1: i1 = i1 + 1: r = 4
- End If
- ElseIf r = 2 Then
- If z = vbYes Then '2-4-1-3
- j1 = j2: i2 = x - i1 + 1: i1 = i1 + 1: r = 4
- Else '4-2-3-1
- j1 = j2: i1 = x - i2 + 1: i2 = i2 - 1: r = 3
- End If
- ElseIf r = 3 Then
- If z = vbYes Then '2-4-1-3-2
- i2 = i1: j2 = y - j1: j1 = j1 + 1: r = 2
- Else '4-2-3-1
- i2 = i1: j1 = y - j2 + 2: j2 = j2 - 1: r = 1
- End If
- ElseIf r = 4 Then
- If z = vbYes Then '2-4-1-3
- i1 = i2: j1 = y - j2 + 1: j2 = j2 - 1: r = 1
- Else '4-2-3-1
- i1 = i2: j2 = y - j1 + 1: j1 = j1 + 1: r = 2
- End If
- End If
- Loop
- [a1].Resize(x, y) = a
- If r Mod 2 Then [a1].Offset(i1 - 1, j1 - 1).Activate Else [a1].Offset(i2 - 1, j2 - 1).Activate
- End Sub
复制代码 |
|