Using system;
Namespace cs.jq {? ///
"/// Magicsquare's summary description. ? ///
PUBLIC CLASS MAGICSQUARE
? {
?? private int n;
??priVate int [,] data;
?? public magicsquare ()
?? {
??? //
??? // Todo: Add constructor logic here
??? //
??}
?? public magicsquare (int N) ?? {??? if (n <= 2) ???? n = 0; ??? Else ??? {???? n = n; ???? Data = new int [n, n]; ???? Cal (); ???} ??}
?? public void Cal () ?? {??? if (n> 1) ??? {???? switch (n% 4) ???? {????? case 1: ???? ? Case 3: ?????? oddcreate (); ?????? a Break; ????? doublyeven (); ?????? breaf; ?? ??? case 2: ?????? singleeven (); ?????? Break; ????} ???} ??}
?? / * odd-numbered steps use SIAMESE constructor * / ????? private void oddcreate () ?? {??? int i, j, value, maxValue; ??? i = 0; ??? j = N / 2; ??? Value = 1; ??? MaxValue = N * n;
??? while (value <= maxValue) ??? {???? data [i, j] = value; ???? = == 0) ????? i ;??? ? Else ???? {????? I -; ????? j ; ????? f (i <0) ?????? i = n; ????? ing (j> = n) ?????? j- = N;
????} ???? value ; ???} ??}
?? / * 4K class fantasy adopted? The following method? * One-n * n fill 1-N * N, and change the number of two diagonal lines of each 4x4 small square to N × N 1-original value? * / ?? private void doubleeven () ?? {??? int maxvalue = n * n; ??? int value = 1;
??? for (int i = 0; i
??? {
???? for (int J = 0; J
???? {
????? int mi = I% 4;
????? int mj = j% 4;
????? IF ((mi == mj) || ((Mi MJ) == 3)) ?????? Data [i, j] = maxValue 1-value; ????? Else ?????? Data [i, j] = value; ????? value ; ????} ???} ??}
?? / * 4K 2 order fantasy, the following method LUX method ?? * first becomes 2K 1 odd stage fantasy, ?? * Front K line adopted L,?? * ????????? ??????? 4 ???? 1 ?? * ????????????????? 2 ???? 3 ?? * The middle line uses L, but the most intermediate U is? * ????????????????? 1 ???? 4 ?? * ???????????????? 2??? ? 3? * K 2 lines use u, but the most intermediate uses L ?? * The rest K-1 line is X, ie ?? * ???????????????? 1? 4? ? * ????????????????? 3 ???? 2 ?? * /
?? private void singleeven () ?? {??? int K = n / 4; ??? int [,] a = {{4, 1, 2, 3}, {1, 4, 2, 3}, {1, 4, 3, 2}}; ??? INT I, J, T, Value, MaxValue; ??? i = 0; ??? j = k; ??? T = 2 * k 1; ??? Value = 1; ??? MaxValue = T * t; ??? while (value <= maxValue) ??? {???? Int i2 = i * 2, j2 = j * 2, value4 = 4 * (Value -1), kind;
???? if (i
????? Kind = 0; ???? Else IF ((i == k) && (j == k) || ((i == k 1) && (j! = k)))? ???? Kind = 1; ???? ELSE ????? Kind = 2;
???? Data [I2, J2] = Value4 a [Kind, 0]; ???? Data [I2, J2 1] = Value4 a [Kind, 1]; ???? Data [i2 1, J2] = Value4 a [Kind, 2]; ???? Data [i2 1, j2 1] = value4 a [Kind, 3];
???? if (value% t == 0) ????? i ; ???? ELSE ???? {????? i -; ????? j ;???? ? IF (i <0) ?????? i = t; ????? IF (j> = t) ?????? j- = t; ????} ???? Value ; ???} ??}
?? public int [,] getResult () ?? {??? Return Data; ??}
?? public string getString () ?? {??? string str = ""; ??? Str = n.toString () "Orchard classification is as follows: / r / n"; ??? for (INT I = 0; i
??? {
???? for (int J = 0; J
????? STR = DATA [i, j] .tostring () "?";
???? STR = "/ r / n";
???}
??? Return Str;
??}
?}?}
Front-end program
<% @ Page language = "c #" codebehind = "square.aspx.cs" autoeventwireup = "false" inherits = "cs.jq.square"%> ?????
??
??
??
http://schemas.microsoft.com/intellisense/ie5 "> ????
?
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls Using system.Web.ui.htmlcontrols;
Namespace cs.jq {? ///
/// Square's summary description. ? ///
PUBLIC CLASS SQUARE: System.Web.ui.page? {
?? protected system.Web.ui.WebControls.TextBox textbox1;
?? protected system.Web.ui.webcontrols.button button1;
?? protected system.web.ui.webcontrols.panel panel1;
?? protected system.web.ui.webcontrols.rangevalidator rangevalidator1;
?? protected system.Web.ui.webcontrols.label label1;
?
?? Private void Page_load (Object Sender, System.EventArgs E)
?? {
??? // Place the user code here to initialize the page
??}
? # Region web form designer generated code ?? Override protected void oninit (Eventargs E) ?? {??? // ??? // codegen: This call is necessary for the ASP.NET Web Form Designer of. ??? // ??? INITIALIZECMPONENT (); ??? Base.onit (E); ??} ???? ////
?? /// Designer supports the required method - do not use the code editor to modify ?? // This method. ?? ///
?? private void initializecomponent ()
?? {???
??? this.button1.click = new system.eventhandler (this.button1_click);
??? this.load = new system.eventhandler (this.page_load);
??} ?? # endregion
?? private void button1_click (object sender, system.eventargs e) ?? {??? Int i = convert.Toint32 (TextBox1.text); ??? int sum = i * (i * i 1) / 2; ??? label1.text = i.toString () "Order" Sum.Tostring (); Sum.Tostring ();
??? int [,] data = new int [i, i]; ??? Data = new magicsquare (i) .getResult (); ??? Table Table = new table (); ??? Table.attributes [ "border"] = "1"; ??? Table.style ["border-collapse"] = "collapse"; ??? Table.cellpadding = 2; ??? Table.cellspacing = 0; ??? //// Table.Width = Unit.Percentage (100); ??? Table.Width = i * 40; ??? Table.backcolor = system.drawing .color .yellow; ?????? for (int N = 0; n
??? {
???? Tablelow hrow = new tableerow ();
???? hrow.height = 40;
???? for (int m = 0; M
???? {
????? Tablecell hcell = new TableCell ();
????? // hcell.width = 30;
????? // hcell.height = 30;
????? hcell.text = data [m, n] .tostring (); ????? hcell.VerticalAlign = system.web .ui .webcontrols .vertageAlign .middle?;
????? hcell.horizontalalign = system.web .ui .webcontrols. Horizontalalign .Center
????? hrow.cells .add (hcell);
????} ???? Table.Rows .add (hrow); ???} ??? panel1.controls .add (table); ??}?}}