今天上班路上,看到新一期电脑报出来了,就随手买了一份来看
也就看到了下面的题目,无事,便写了一段
题目如下:
对如下矩阵,要求经过四次顺时针旋转,最后又得到最初的矩阵,并显示过程。
123 741 987 369 123
456 > 852 > 654 > 258 > 456
789 963 321 147 789
我的解题思路很简易,直接抽取对像来对比一下就能看出规律来:
第一个矩阵我用二维数组a(2,2)表示,第二个矩阵用二维数组b(2,2)表示,对比结果如下:
b(0,0)=a(2,0)
b(0,1)=a(1,0)
b(0,2)=a(0,0)
b(1,0)=a(2,1)
b(1,1)=a(1,1)
b(1,2)=a(0,1)
b(2,0)=a(2,2)
b(2,1)=a(1,2)
b(2,2)=a(0,2)
很明显的规律了,看下标的变化哈,然后就有了下面的程序了,vb.net的
删去了窗体设计代码,一个FORM1,一个Textbox1,一个BUTTON1,一个TIMER1。
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
Public Class Form1Class Form1
Inherits System.Windows.Forms.Form
Public a, b As Integer(,) '定义两个二维数组
#Region " Windows 窗体设计器生成的代码 "
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Sub New()Sub New()
MyBase.New()
![](/Images/OutliningIndicators/InBlock.gif)
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
![](/Images/OutliningIndicators/InBlock.gif)
'在 InitializeComponent() 调用之后添加任何初始化
![](/Images/OutliningIndicators/InBlock.gif)
End Sub
![](/Images/OutliningIndicators/InBlock.gif)
'窗体重写 dispose 以清理组件列表。
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
![](/Images/OutliningIndicators/InBlock.gif)
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
![](/Images/OutliningIndicators/InBlock.gif)
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Timer1 As System.Windows.Forms.Timer
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.Button1 = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(24, 112)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(80, 32)
Me.Button1.TabIndex = 0
Me.Button1.Text = "开始演示"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(24, 32)
Me.TextBox1.Multiline = True
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(80, 72)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = ""
Me.TextBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
'
'Timer1
'
Me.Timer1.Interval = 1000
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(128, 165)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
![](/Images/OutliningIndicators/InBlock.gif)
End Sub
![](/Images/OutliningIndicators/InBlock.gif)
#End Region
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Sub print()Sub print() '在文本框里显示结果的过程
TextBox1.Text = Nothing
Dim i, j As Integer
For i = 0 To 2
For j = 0 To 2
TextBox1.Text += CStr(a(i, j))
Next
TextBox1.Text += vbCrLf
Next
End Sub
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
a = New Integer(2, 2) {} '重新定义a下标值
b = New Integer(2, 2) {} '重新定义b下标值
Dim i, j, g As Integer
For i = 0 To 2 '循环方式初始化数组a的值
For j = 0 To 2
g += 1
a(i, j) = g
Next
Next
Timer1.Enabled = True '启动时间控件
End Sub
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Private Sub Timer1_Tick()Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
print() '调用显示过程
Dim i, j As Integer
For i = 0 To 2 '利用循环给数组了赋值
For j = 0 To 2
b(i, j) = a(2 - j, i)
Next
Next
For i = 0 To 2 '因为直接a=b的只是句柄相等了,所以用循环重新给数组a赋值
For j = 0 To 2
a(i, j) = b(i, j)
Next
Next
![](/Images/OutliningIndicators/InBlock.gif)
End Sub
End Class