posts - 56,  comments - 12,  trackbacks - 0

          魔術(shù)矩陣是一個n*n的矩陣,其中N必須為奇數(shù),然后將1至n的平方的整數(shù)依照指定的規(guī)則放入矩陣,完成后矩陣的各行各列以及對角線的元素值總和均會相同。

          放置規(guī)則,第一個整數(shù)1一律被放置在第一行的中間位置,第二個值得位置必須放置在第一個值得左上角,因此,由1的位置往左一格,再往上一格,此時超出數(shù)組的范圍,因此回到同一列的最下方。

          同樣,3必須放在2的左上角,4放在3的左上角,此時超出數(shù)組范圍回到同一行的最右邊空格。同此規(guī)則,來到5的位置,當(dāng)我們要填入6的時候,其位置 已被數(shù)字1占據(jù),因此將其放置到5的下方,接下來的數(shù)字則依規(guī)則一一放置到合適的位置,當(dāng)填到15的時候,其左方與上方均沒有位置,因此下一個數(shù)直接放置 于15的下方,完成了填入16的值,接下來的數(shù)目,均可以依上述的規(guī)則填寫完成。

          下面看看這個魔術(shù)矩陣的示例代碼:

          /*此類是主窗口,接受輸入的奇數(shù)

          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>
            /// 必需的設(shè)計(jì)器變量。
            /// </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 窗體設(shè)計(jì)器支持所必需的
             //
             InitializeComponent();

             //
             // TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
             //
            }

            /// <summary>
            /// 清理所有正在使用的資源。
            /// </summary>
            protected override void Dispose( bool disposing )
            {
             if( disposing )
             {
              if (components != null)
              {
               components.Dispose();
              }
             }
             base.Dispose( disposing );
            }

            #region Windows 窗體設(shè)計(jì)器生成的代碼
            /// <summary>
            /// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
            /// 此方法的內(nèi)容。
            /// </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 = "請輸入奇數(shù):";
             //
             // 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>
            /// 應(yīng)用程序的主入口點(diǎn)。
            /// </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("您輸入的不是奇數(shù),請重新輸入奇數(shù)!","輸入錯誤");
             }
            }
           }
          }

          /*這個類用來處理魔術(shù)矩陣的生成,以及在窗體上顯示出來

          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>
            /// 必需的設(shè)計(jì)器變量。
            /// </summary>
            private System.ComponentModel.Container components = null;
            private int n;
            private int[,] mm;
            public ShowResult ()
            {
             //
             // Windows 窗體設(shè)計(jì)器支持所必需的
             //
             InitializeComponent();

             //
             // TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
             //
            }
            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 窗體設(shè)計(jì)器生成的代碼
            /// <summary>
            /// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
            /// 此方法的內(nèi)容。
            /// </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;
             //初始化數(shù)組
             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 發(fā)表評論

          文章來源:http://www.aygfsteel.com/kuxiaoku/articles/94805.html
          posted on 2007-01-19 00:14 苦笑枯 閱讀(1043) 評論(0)  編輯  收藏 所屬分類: C#
          收藏來自互聯(lián)網(wǎng),僅供學(xué)習(xí)。若有侵權(quán),請與我聯(lián)系!

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

          常用鏈接

          留言簿(2)

          隨筆分類(56)

          隨筆檔案(56)

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 张家川| 太仆寺旗| 渑池县| 崇左市| 上思县| 南康市| 疏勒县| 新干县| 大埔县| 扎兰屯市| 安西县| 江阴市| 长武县| 武冈市| 务川| 静安区| 尤溪县| 罗城| 车险| 禹城市| 桃园市| 江西省| 八宿县| 汉中市| 富川| 扶绥县| 新建县| 达尔| 贡嘎县| 台山市| 巨野县| 海盐县| 盖州市| 舟曲县| 南华县| 白山市| 太白县| 永嘉县| 自治县| 龙胜| 龙海市|