魔術矩陣是一個n*n的矩陣,其中N必須為奇數,然后將1至n的平方的整數依照指定的規則放入矩陣,完成后矩陣的各行各列以及對角線的元素值總和均會相同。
放置規則,第一個整數1一律被放置在第一行的中間位置,第二個值得位置必須放置在第一個值得左上角,因此,由1的位置往左一格,再往上一格,此時超出數組的范圍,因此回到同一列的最下方。
同樣,3必須放在2的左上角,4放在3的左上角,此時超出數組范圍回到同一行的最右邊空格。同此規則,來到5的位置,當我們要填入6的時候,其位置
已被數字1占據,因此將其放置到5的下方,接下來的數字則依規則一一放置到合適的位置,當填到15的時候,其左方與上方均沒有位置,因此下一個數直接放置
于15的下方,完成了填入16的值,接下來的數目,均可以依上述的規則填寫完成。
下面看看這個魔術矩陣的示例代碼:
/*此類是主窗口,接受輸入的奇數
MagicSquare.cs
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace MagicSquare
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class MagicSquare : System.Windows.Forms.Form
{
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Label lblNumber;
private System.Windows.Forms.TextBox txbNumber;
private System.Windows.Forms.Button btnOK;
public int number=0;
public MagicSquare()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調用后添加任何構造函數代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.lblNumber = new System.Windows.Forms.Label();
this.txbNumber = new System.Windows.Forms.TextBox();
this.btnOK = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lblNumber
//
this.lblNumber.Location = new System.Drawing.Point(48, 32);
this.lblNumber.Name = "lblNumber";
this.lblNumber.TabIndex = 0;
this.lblNumber.Text = "請輸入奇數:";
//
// txbNumber
//
this.txbNumber.Location = new System.Drawing.Point(192, 32);
this.txbNumber.Name = "txbNumber";
this.txbNumber.TabIndex = 1;
this.txbNumber.Text = "";
//
// btnOK
//
this.btnOK.Location = new System.Drawing.Point(144, 80);
this.btnOK.Name = "btnOK";
this.btnOK.TabIndex = 2;
this.btnOK.Text = "OK";
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// MagicSquare
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(432, 117);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.txbNumber);
this.Controls.Add(this.lblNumber);
this.Name = "MagicSquare";
this.Text = "MagicSquare";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MagicSquare());
}
private void btnOK_Click(object sender, System.EventArgs e)
{
number=Convert.ToInt32(txbNumber.Text.Trim());
if(number%2==1)
{
this.Hide();
ShowResult result=new ShowResult(number);
result.Show();
}
else
{
MessageBox.Show("您輸入的不是奇數,請重新輸入奇數!","輸入錯誤");
}
}
}
}
/*這個類用來處理魔術矩陣的生成,以及在窗體上顯示出來
Show.cs
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace MagicSquare
{
/// <summary>
/// Show 的摘要說明。
/// </summary>
public class ShowResult : System.Windows.Forms.Form
{
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
private int n;
private int[,] mm;
public ShowResult ()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調用后添加任何構造函數代碼
//
}
public ShowResult (int number)
{
n=number;
mm=new int[n,n];
AssignValue(n);
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
Application.Exit();
}
#region Windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "ShowResult";
}
#endregion
public void AssignValue(int n)
{
int assignValue=1;
int p=n-1;
int col=p/2;
int row=0;
//初始化數組
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
mm[i,j]=0;
}
}
mm[row,col]=assignValue;
do
{
assignValue++;
col--;
row--;
if(col<0&&row<0)
{
row+=2;
col+=1;
}
else
{
if(col<0)
col=p;
if(row<0)
row=p;
}
if(mm[row,col]!=0)
{
col+=1;
row+=2;
}
mm[row,col]=assignValue;
}while(assignValue<n*n);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
Graphics g=this.CreateGraphics();
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
g.DrawString(mm[i,j].ToString(),new Font(FontFamily.GenericSansSerif.ToString(),15f),Brushes.YellowGreen,j*40+20,i*40+20);
}
g.Dispose();
}
}
}
文章來源:
http://www.tkk7.com/kuxiaoku/articles/94805.html
posted on 2007-01-19 00:14
苦笑枯 閱讀(1032)
評論(0) 編輯 收藏 所屬分類:
C#