打印魔方(幻方、九宫图)C#源码

  1. using System;
  2. namespace MagicMatrix
  3. {
  4.     class CRunMain
  5.     {
  6.         public static int Main()
  7.         {
  8.             int nWidth = 0;
  9.             CPrintMatrix objMat;
  10.             Console.WriteLine("please input maxtrix width: ");
  11.             nWidth = int.Parse(Console.ReadLine());
  12.             objMat = new CPrintMatrix(nWidth);
  13.             objMat.PrintMaxtrix();
  14.             return 0;
  15.         }
  16.     };
  17.     class CPrintMatrix
  18.     {
  19.         private int m_nWidth = 0;
  20.         private int [ ,] m_arrMatrix;
  21.         public CPrintMatrix(int nWidth)
  22.         {
  23.             this.m_nWidth = nWidth;
  24.         }
  25.         public void SetWidth(int nWdith)
  26.         {
  27.             this.m_nWidth = nWdith;
  28.         }
  29.         public void PrintMaxtrix()
  30.         {
  31.             int i = 0;
  32.             int j = 0;
  33.             if (!this.SettMatrix())
  34.             {
  35.                 return;
  36.             }
  37.             for (i = 0; i < this.m_nWidth; i++)
  38.             {
  39.                 for (j = 0; j < this.m_nWidth; j++)
  40.                 {
  41.                     Console.Write("{0}/t"this.m_arrMatrix[i, j]);
  42.                 }
  43.                 Console.WriteLine();
  44.             }
  45.         }
  46.         public bool SettMatrix()
  47.         {
  48.             int i = 0;
  49.             int j = 0;
  50.             int k = 0;
  51.             int ii = 0;
  52.             int jj = 0;
  53.             if (0 >= this.m_nWidth)
  54.             {
  55.                 Console.WriteLine("width must above 0!current value is {0}"this.m_nWidth);
  56.                 return false;
  57.             }
  58.             if (this.m_nWidth % 2 == 0)
  59.             {
  60.                 Console.WriteLine("width must odd number! current value is {0}"this.m_nWidth);
  61.                 return false;
  62.             }
  63.             //new matricx
  64.             this.m_arrMatrix = new int[this.m_nWidth, this.m_nWidth];
  65.             //init matrix
  66.             for (i = 0; i < this.m_nWidth; i++)
  67.             {
  68.                 for (j = 0; j < this.m_nWidth; j++)
  69.                 {
  70.                     this.m_arrMatrix[i, j] = 0;
  71.                 }
  72.             }
  73.             //set value
  74.             this.m_arrMatrix[0, this.m_nWidth/2] = 1;
  75.             i = 0;
  76.             j = this.m_nWidth / 2;
  77.             for (k = 1; k < this.m_nWidth * this.m_nWidth; k++)
  78.             {
  79.                 if ( (0 == i) && (0 == j) )
  80.                 {
  81.                     this.m_arrMatrix[1, 0] = k + 1;
  82.                     i = 1;
  83.                     j = 0;
  84.                     continue;
  85.                 }
  86.                 ii = (i + this.m_nWidth - 1) % this.m_nWidth;
  87.                 jj = (j + this.m_nWidth - 1) % this.m_nWidth;
  88.  
  89.                 if (0 == this.m_arrMatrix[ii, jj])
  90.                 {
  91.                     this.m_arrMatrix[ii, jj] = k + 1;
  92.                     i = ii;
  93.                     j = jj;
  94.                 }
  95.                 else 
  96.                 {
  97.                     ii = (i + 1) % this.m_nWidth;
  98.                     jj = j;
  99.                     this.m_arrMatrix[ii, jj] = k + 1;
  100.                     
  101.                     i = ii;
  102.                     j = jj;
  103.                 }
  104.             }
  105.             return true;
  106.         }
  107.     };
  108. }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值