CSDN博客

img gj0001

C#实现的多阶幻方,由C改动而来

发表于2004/7/13 12:18:00  823人阅读

using System;

namespace cs.jq
{
?///


?/// MagicSquare 的摘要说明。
?///

?public class MagicSquare
?{
??private int N;
??private int[,] Data;
??public MagicSquare()
??{
???//
???// TODO: 在此处添加构造函数逻辑
???//
??}

??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();
??????break;
?????case 0:
??????doublyEven();
??????break;
?????case 2:
??????singleEven();
??????break;
????}
???}
??}

??/*奇数阶幻方采用Siamese 构造方法*/???
??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;
????if (Value%N==0)
?????i++;
????else
????{
?????i--;
?????j++;
?????if (i<0)
??????i+=N;
?????if (j>=N)
??????j-=N;

????}
????Value++;
???}
??}

??/* 4K阶幻方采用?如下方法
? * 一次填充1-N*N,并将每个4X4小方格的两对角线上的数换成N×N+1-原来值
? */
??private void doublyEven()
??{
???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 阶幻方 如下方法LUX方法
?? * 先变成2K+1奇数阶幻方,
?? *前面K行采用L 即?
?? *???????????????? 4???? 1
?? *???????????????? 2???? 3
?? *中间一行采用L,但最中间采用U 即
?? *???????????????? 1???? 4
?? *???????????????? 2???? 3
?? *K+2行采用U,但最中间采用L
?? *其余K-1行采用X ,即
?? *???????????????? 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 ()+"阶幻方构造如下:/r/n";
???for (int i=0;i???{
????for(int j=0;j?????str+=Data[i,j].ToString ()+"? ";
????str+="/r/n";
???}
???return str;
??}

?}
? }

前端程序

<%@ Page language="c#" Codebehind="Square.aspx.cs" AutoEventWireup="false" Inherits="cs.jq.Square" %>


?
??
??
??
??
?? http://schemas.microsoft.com/intellisense/ie5">
?
?
??


???
???????Text="幻? 方">
???????Width="312px" Height="18px">
???????Width="376px" Height="280px" ForeColor="White">Panel
???????runat="server" ErrorMessage="输入3-100的数字" ControlToValidate="TextBox1" MaximumValue="100" MinimumValue="3"
????Type="Integer" Display="Dynamic">
??

?

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 的摘要说明。
?///

?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)
??{
???// 在此处放置用户代码以初始化页面
??}

??#region Web 窗体设计器生成的代码
??override protected void OnInit(EventArgs e)
??{
???//
???// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
???//
???InitializeComponent();
???base.OnInit(e);
??}
??
??///


??/// 设计器支持所需的方法 - 不要使用代码编辑器修改
??/// 此方法的内容。
??///

??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 ()+"阶幻方构造如下行列对角求和为:"+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???{
????TableRow hrow=new TableRow ();
????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 .VerticalAlign .Middle? ;
?????hcell.HorizontalAlign =System.Web .UI .WebControls .HorizontalAlign .Center ;
?????hrow.Cells .Add (hcell);

????}
????table.Rows .Add (hrow);
???}
???Panel1.Controls .Add (table);
??}
?}
}

0 0

相关博文

我的热门文章

img
取 消
img