posts - 56,  comments - 12,  trackbacks - 0

魔术矩阵是一个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();

  }

 }
}

苦笑枯 2007-01-19 00:14 发表评论

文章来源:http://www.blogjava.net/kuxiaoku/articles/94805.html
posted on 2007-01-19 00:14 苦笑枯 阅读(981) 评论(0)  编辑  收藏 所属分类: C#

只有注册用户登录后才能发表评论。


网站导航:
 
收藏来自互联网,仅供学习。若有侵权,请与我联系!

<2007年1月>
31123456
78910111213
14151617181920
21222324252627
28293031123
45678910

常用链接

留言簿(2)

随笔分类(56)

随笔档案(56)

搜索

  •  

最新评论

阅读排行榜

评论排行榜