<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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.tkk7.com/kuxiaoku/articles/94805.html
    posted on 2007-01-19 00:14 苦笑枯 閱讀(1032) 評論(0)  編輯  收藏 所屬分類: C#
    收藏來自互聯網,僅供學習。若有侵權,請與我聯系!

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

    常用鏈接

    留言簿(2)

    隨筆分類(56)

    隨筆檔案(56)

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 乱人伦中文视频在线观看免费| 亚洲国产精品久久久久秋霞影院 | 国产精品亚洲专区无码唯爱网| 亚洲视频免费在线观看| 在线观看午夜亚洲一区| 99精品免费视频| 亚洲短视频男人的影院| 四虎国产精品永久免费网址| 老色鬼久久亚洲AV综合| 91禁漫免费进入| 亚洲一区二区三区无码国产 | 美女无遮挡免费视频网站| 国产小视频免费观看| 亚洲av无码成人精品国产 | 亚洲免费人成视频观看| 91视频国产免费| 亚洲欧美国产国产综合一区| 国产成人精品男人免费| 深夜a级毛片免费无码| 国产gv天堂亚洲国产gv刚刚碰| 青青操视频在线免费观看| 亚洲国产精品第一区二区| 日本人的色道免费网站| 亚洲AV无码AV吞精久久| 亚洲中文字幕在线观看| 久久成人免费播放网站| 亚洲成a人片在线不卡| 国产成人精品男人免费| 精选影视免费在线 | 久久亚洲私人国产精品| 午夜毛片不卡免费观看视频| 一个人免费观看视频在线中文| 亚洲国产精品无码久久久秋霞2 | a在线视频免费观看| 亚洲日本国产乱码va在线观看| 永久黄网站色视频免费观看| 中文字幕在线免费视频| 亚洲一区中文字幕在线电影网| 亚洲va中文字幕无码| 最近最好最新2019中文字幕免费| 亚洲AV色欲色欲WWW|