{"id":11903,"date":"2016-11-22T00:00:00","date_gmt":"2016-11-21T16:00:00","guid":{"rendered":"https:\/\/fgchen.com\/wpedu2\/2016\/11\/22\/%e3%80%90c-sharp%e7%a8%8b%e5%bc%8f%e8%a8%ad%e8%a8%88%e3%80%91%e9%ad%94%e6%96%b9%e9%99%a3\/"},"modified":"2026-03-30T14:54:33","modified_gmt":"2026-03-30T06:54:33","slug":"%e3%80%90c-sharp%e7%a8%8b%e5%bc%8f%e8%a8%ad%e8%a8%88%e3%80%91%e9%ad%94%e6%96%b9%e9%99%a3","status":"publish","type":"post","link":"https:\/\/fgchen.com\/wpedu\/2016\/11\/%e3%80%90c-sharp%e7%a8%8b%e5%bc%8f%e8%a8%ad%e8%a8%88%e3%80%91%e9%ad%94%e6%96%b9%e9%99%a3\/","title":{"rendered":"\u3010\u7a0b\u5f0f\u8a2d\u8a08-C#\u3011\u9b54\u65b9\u9663"},"content":{"rendered":"<h2>\u9b54\u65b9\u9663\u7684\u89e3\u6cd5\/\u6f14\u7b97\u6cd5\uff1a<\/h2>\n\n\u8a2d\u6709\u5947\u6578\u65b9\u9663\uff0c3&#215;3, 5&#215;5&#8230;, nxn\n\u8981\u5c071~nxn\u7684\u6578\u503c\u586b\u5165\u6b64\u65b9\u9663\u4e2d\uff0c\u4f7f\u5f97\u6b64\u65b9\u9663\u7684\u5404\u5217\u3001\u5404\u884c\u3001\u659c\u884c\/\u5217\u4e0a\u7684\u6578\u503c\u52a0\u7e3d\u70ba\u76f8\u7b49\u3002\nStep 1: \u5c071\u7f6e\u653e\u65bc\u65b9\u9663\u7684\u7b2c1\u5217\u4e2d\u9593\u4f4d\u7f6e\n\u8a2dN= 2\u00a0 &#8230; nxn\nStep 2: \u5f80\u5de6\u4e0a\u89d2\u627e\u5019\u9078\u7684\u4f4d\u7f6e\u4f86\u653e\u5165N\uff0c\u6b64\u6642\u5de6\u4e0a\u89d2\u7684\u4f4d\u7f6e\uff0c\u6703\u6709\u5e95\u4e0b2\u7a2e\u72c0\u6cc1\n\u72c0\u6cc11: \u5de6\u4e0a\u89d2\u7684\u4f4d\u7f6e\u8dd1\u51fa\u9663\u5217\u5916\uff0c\u6b64\u6642\uff0c\u6211\u5011\u5c07\u5de6\u4e0a\u89d2\u4f4d\u7f6e\u53e6\u4e00\u7aef\u4f4d\u7f6e\u505a\u70ba\u65b0\u5019\u9078\u4f4d\u7f6e\u3002\n\u72c0\u6cc12\uff1a\u82e5\u5019\u9078\u4f4d\u7f6e\u4e0a\u5df2\u7d93\u88ab\u4f54\u64da\u4e86(\u5df2\u6709\u6578\u503c)\uff0c\u6b64\u6642\uff0c\u6211\u5011\u5c07\u539f\u672c\u6578\u503c\u4f4d\u7f6e\u7684\u4e0b\u65b9\u4f4d\u7f6e\uff0c\u505a\u70ba\u65b0\u5019\u9078\u4f4d\u7f6e\u3002\nStep 3: \u5c07\u6578\u503cN\u653e\u5165\u5019\u9078\u4f4d\u7f6e\/\u65b0\u5019\u9078\u4f4d\u7f6e (\u627e\u5230\u7684\u53ef\u653e\u7f6e\u6578\u503c\u4f4d\u7f6e)\u3002\nStep 4\uff1aN\u52a01\uff0c\u91cd\u8986Step 2\u8207Step 4\uff0c\u76f4\u5230N\u7b49\u65bcnxn\u3002\n\n<hr \/>\n\nif &amp; \u53d6\u9918\u6578\u5faa\u74b0\u7248\n\n<pre class=\"theme:classic lang:c# decode:true \">using System;\nusing System.Collections.Generic;\nusing System.ComponentModel;\nusing System.Data;\nusing System.Drawing;\nusing System.Linq;\nusing System.Text;\nusing System.Threading.Tasks;\nusing System.Windows.Forms;\nnamespace WindowsFormsApplication2\n{\n    public partial class Form1 : Form\n    {\n        public Form1()\n        {\n            InitializeComponent();\n        }\n        private void makeMagicMatrix1(int matrixSize)\n        {\n            int[,] Matrix = new int[99, 99];\n            int row = 0;\n            int col = matrixSize \/ 2;\n            Matrix[row, col] = 1;\n            for (int stepper = 2; stepper &lt;= matrixSize * matrixSize; stepper++)\n            {\n                row--;col--; \/\/\u5f80\u5de6\u4e0a\u89d2\u79fb\u52d5\uff0c\u4e8c\u7dad\u9663\u5217\u884c\u8207\u5217\u7d22\u5f15\u7686\u6e1b1\n                \/\/\u5224\u65b7\u662f\u5426\u51fa\u754c\uff0c\u5c0f\u65bc0\u70ba\u51fa\u754c\u7684\u689d\u4ef6\n                if (row &lt; 0) row = matrixSize - 1;\n                if (col &lt; 0) col = matrixSize - 1;\n                if (Matrix[row, col] != 0) \/\/\u5224\u65b7\u8981\u653e\u7f6e\u7684\u4f4d\u8655\u82e5\u4e0d\u662f\u7a7a\u7684\u8a71\uff0c\u9700\u8981\u9032\u884c\u5012\u9000\u7684\u7684\u52d5\u4f5c\n                {\n                    row = (row + 2);\n                    col = (col + 1);\n                    \/\/\u5012\u9000\u5f8c\uff0c\u4e5f\u6709\u53ef\u80fd\u51fa\u754c\uff0c\u5927\u65bcmatrixSize-1\u70ba\u51fa\u754c\u7684\u689d\u4ef6\n                    if (row &gt;= matrixSize) row = row - matrixSize;\n                    if (col &gt;= matrixSize) col = col - matrixSize;\n                }\n                Matrix[row, col] = stepper;\n            }\n            string outStrng = \"\";\n            for (int i = 0; i &lt; matrixSize; i++)\n            {\n                for (int j = 0; j &lt; matrixSize; j++)\n                {\n                    outStrng += Matrix[i, j].ToString().PadLeft(2, ' ') + \"   \";\n                }\n                outStrng += \"rn\";\n            }\n            label2.Text = outStrng;\n            label2.Location = new Point((this.panel2.Width - label2.Width) \/ 2, 0); \/\/\u7f6e\u4e2dlabel2\n        }\n        private void makeMagicMatrix2(int matrixSize)\n        {\n            int[,] Matrix = new int[99, 99];\n            int row = 0;\n            int col = matrixSize \/ 2;\n            Matrix[row, col] = 1;\n            for (int stepper = 2; stepper &lt;= matrixSize * matrixSize; stepper++)\n            {\n                row = (row - 1 + matrixSize) % matrixSize;\n                col = (col - 1 + matrixSize) % matrixSize;\n                if (Matrix[row, col] != 0)\n                {\n                    row = (row + 2) % matrixSize;\n                    col = (col + 1) % matrixSize;\n                }\n                Matrix[row, col] = stepper;\n            }\n            string outStrng = \"\";\n            for (int i = 0; i &lt; matrixSize; i++)\n            {\n                for (int j = 0; j &lt; matrixSize; j++)\n                {\n                    outStrng += Matrix[i, j].ToString().PadLeft(2, ' ') + \"   \";\n                }\n                outStrng += \"rn\";\n            }\n            label2.Text = outStrng;\n            label2.Location = new Point((this.panel2.Width - label2.Width) \/ 2, 0); \/\/\u7f6e\u4e2dlabel2\n        }\n        private void button1_Click(object sender, EventArgs e)\n        {\n            int matrixSize = Convert.ToInt32(textBox1.Text);\n            makeMagicMatrix1(matrixSize);\n        }\n    }\n}\n<\/pre>\n\n&nbsp;\n<img fetchpriority=\"high\" decoding=\"async\" class=\"size-full wp-image-3083 aligncenter\" src=\"http:\/\/www.fgchen.com\/wp\/wp-content\/uploads\/2016\/11\/magicMatrixFlow.jpg\" alt=\"magicmatrixflow\" width=\"784\" height=\"1030\" \/>\n\n<h2>2018\/10\/24 \u7a0b\u5f0f\u78bc<\/h2>\n\n<pre>\nconst int n = 5;\nint[,] M = new int[n, n];\nint row = 0, col = n \/ 2;\nM[row, col] = 1; \/\/\u5c071\u653e\u5165\u7b2c1\u5217\u4e2d\u9593\u4f4d\u7f6e\nfor (int N = 2; N &lt;= n * n; N++)\n{\n    \/\/\u72c0\u6cc11\n    row = (row - 1 + n) % n; col = (col - 1 + n) % n;\n    \/\/\u72c0\u6cc12\n    if (M[row, col] != 0) \/\/\u8981\u653e\u7684\u4f4d\u7f6e\u4e0a\u5df2\u7d93\u6709\u6578\u503c\n    {\n        row = (row + 2) % n; col = (col + 1) % n;\n    }\n    M[row, col] = N; \/\/\u5c07N\u653e\u5165\u6c7a\u5b9a\u597d\u7684\u4f4d\u7f6e\n}\n\/\/\u8f38\u51fa\u9b54\u65b9\u9663\nfor (int r = 0; r &lt; n; r++)\n{\n    for (int c = 0; c &lt; n; c++)\n    {\n        Console.Write(&quot;{0, 4}&quot;, M[r, c]);\n    }\n    Console.WriteLine();\n}\n<\/pre>","protected":false},"excerpt":{"rendered":"<p>\u9b54\u65b9\u9663\u7684\u89e3\u6cd5\/\u6f14\u7b97\u6cd5\uff1a \u8a2d\u6709\u5947\u6578\u65b9\u9663\uff0c3&#215;3, 5&#215;5&#038;#8 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","fifu_image_url":"","fifu_image_alt":"","footnotes":""},"categories":[266],"tags":[],"class_list":["post-11903","post","type-post","status-publish","format-standard","hentry","category-266"],"_links":{"self":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/posts\/11903","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/comments?post=11903"}],"version-history":[{"count":1,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/posts\/11903\/revisions"}],"predecessor-version":[{"id":13411,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/posts\/11903\/revisions\/13411"}],"wp:attachment":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/media?parent=11903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/categories?post=11903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/tags?post=11903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}