<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)

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 五级黄18以上免费看| 黄网站色视频免费观看45分钟| 东北美女野外bbwbbw免费| 免费一级黄色毛片| 日韩国产欧美亚洲v片| 热99re久久免费视精品频软件 | 亚洲综合色区在线观看| 免费大片黄在线观看| 亚洲国产成人精品女人久久久 | 特级av毛片免费观看| 凹凸精品视频分类国产品免费| 亚洲丁香婷婷综合久久| 青草草在线视频永久免费| 亚洲国产成人精品无码区花野真一| 无码人妻一区二区三区免费| 亚洲精品456人成在线| 热久久精品免费视频| 深夜A级毛片视频免费| 久久精品国产亚洲一区二区三区| 在线观看黄片免费入口不卡| 久久伊人久久亚洲综合| 亚洲啪啪免费视频| 亚洲欧美日韩中文二区| 亚洲AV无码一区二区三区在线观看 | 国产免费人人看大香伊| 色婷婷精品免费视频| 亚洲日韩av无码| 免费观看无遮挡www的视频| 亚洲熟女综合色一区二区三区| 国产免费变态视频网址网站| 三级黄色在线免费观看| 亚洲第一成年人网站| 日本免费人成黄页网观看视频| caoporn成人免费公开| 久久亚洲AV成人无码国产| 无码人妻精品一二三区免费| 乱爱性全过程免费视频| 97亚洲熟妇自偷自拍另类图片 | 久久精品免费观看| 亚洲一区二区三区不卡在线播放| 成人永久免费高清|