显示,用C#实现 当n=2 n=3 n=4 n=...1 2 1 2 3 1 2 3 44 3 8 9 4 12 13 14 5 7 6 5 11 16 15 6 10 9 8 7 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这在ACM题目里算简单的了有点取巧的办法就是建个大二维数组 然后逐个填充各个位就可以了 using System;namespace Temp{ /// <summary> /// Class1 的摘要说明。 /// </summary> class Class1 { static int n = 5; static int[,] Num; /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main(string[] args) { Class1 Show = new Class1(); Show.ShowNumber(n); System.Console.Read(); } void ShowNumber(int n) { int count = 0; int x=1; int y=n/2; Num = new int[n,n]; if ((n % 2)!=0) { Num[y,y] = n*n; } for(int i=0;i<4;i++) { for(int j=count;j<n-count-1;j++) { Num[count,j]=x++; } for(int j=count;j<n-count-1;j++) { Num[j,n-count-1]=x++; } for(int j=n-count-1;j>count;j--) { Num[n-count-1,j]=x++; } for(int j=n-count-1;j>count;j--) { Num[j,count]=x++; } count++; } for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { System.Console.Write(Num[i,j]+" "); } System.Console.Write("\n"); } } }} 试一试:private void DoTest(int vNum) //vNum = n{ int[,] ldata; int ltmp = 0,i,n; ldata = new int[vNum,vNum]; for(n=1;n <= vNum*2;n++) { i=n/2 - n/4; for(;i<vNum - n/4;i++) { ltmp ++; ldata[(n-1)/4,i] = ltmp; } RotateArray(ref ldata); } for(n=1;n<=vNum%2 *2;n++) RotateArray(ref ldata); for(n=0;n<vNum;n++) { for(i=0;i<vNum;i++) System.Console.Write(ldata[n,i].ToString() + "\t"); System.Console.WriteLine(); }} private void RotateArray(ref int[,] vData){ int[,] ldata; ldata = new int[vData.GetLength(0),vData.GetLength(1)]; for(int n=0;n<vData.GetLength(0);n++) for(int i=0;i<vData.GetLength(1);i++) { ldata[vData.GetLength(1) - 1 - i,n] = vData[n,i]; } vData = ldata;} lgyangell() : 试一下showNumber(n>=10),有问题; 不会吧~~我回家试试~公司没有.net!~~~晚上时间短没有自己检查~~ private class FillArrays { public FillArrays(int n) { this.n = n; num = new int[n,n]; this.Fill(); } private int n; private int x = 0; private int y = 0; private int[,] num; PointTo pt = PointTo.Right ; public int[,] Result { get { return num; } } private void PointToRight() { pt = PointTo.Right; y++; x++; } private void PointToLeft() { pt = PointTo.Left; y--; x--; } private void PointToDown() { pt = PointTo.Down; x++; y--; } private void PointToUp() { pt = PointTo.Up; x--; y++; } private void Fill() { for(int i=1; i<= n*n; i++) { do { if (pt == PointTo.Right) { if (y == n) PointToDown(); else { if (num[x,y] == 0) { num[x,y] = i; y++; break; } else PointToDown(); } } if (pt == PointTo.Down) { if (x == n) PointToLeft(); else { if (num[x,y] == 0) { num[x,y] = i; x++; break; } else PointToLeft(); } } if (pt == PointTo.Left) { if (y < 0) PointToUp(); else { if (num[x,y] == 0) { num[x,y] = i; y--; break; } else PointToUp(); } } if (pt == PointTo.Up) { if (x < 0) PointToRight(); else { if (num[x,y] == 0) { num[x,y] = i; x--; break; } else PointToRight(); } } } while(true); } } } using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication1{ class Program { static int width; static int square; static void Main(string[] args) { int n; while ((n = Convert.ToInt32(Console.ReadLine())) != 0) { square = n; int idx = 1; width = (n * n).ToString().Length + 1; Console.Clear(); for (int i = 0; i < (n + 1) / 2; i++) { Print(n - i * 2, ref idx); } } } private static void Print(int n, ref int idx) { int lt = (square - n) / 2 ; if (n == 1) { Console.SetCursorPosition(lt * width, lt); Console.Write(idx++); Console.SetCursorPosition(0, square); } for (int i = 0; i < n-1; i++) { Console.SetCursorPosition((lt+i)*width, lt); Console.Write(idx++); } for (int i = 0; i < n - 1; i++) { Console.SetCursorPosition((lt + n - 1)*width, lt + i); Console.WriteLine(idx++); } for (int i = 0; i < n - 1; i++) { Console.SetCursorPosition((lt + n - 1 - i )* width, lt + n - 1); Console.WriteLine(idx++); } for (int i = 0; i < n - 1; i++) { Console.SetCursorPosition(lt*width, lt + n - 1 - i); Console.WriteLine(idx++); if (n == 2) Console.SetCursorPosition(0, square); } } }} pupo(泡泡) ,请教一下,PointTo是哪一个命名空间的?我找了好久都没找到,真晕哦! C#输入框可选可输入 学习C#,很难进大公司? 关于GridView控件绑定数据 调用.dll 在vs2005中winForm里的listBox及comBox中怎么样才可以加图片及文字啊.(我要一起加),帮顶有分 [^关于滚动条的问题^] 急 救~~~~~~ 求大神帮我弄个树谢啦 有C#方面的反编译工具吗!? 【论论】WPF中用什么控件承载应用程序的“设置(选项)”窗体中右侧的设置区域内容 请问各位大虾一个关于流加密的问题 Image Photo = new Bitmap(string filename) 问一个关于线程的问题,加深一下理念
有点取巧的办法就是建个大二维数组 然后逐个填充各个位就可以了
namespace Temp
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
static int n = 5;
static int[,] Num; /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Class1 Show = new Class1();
Show.ShowNumber(n);
System.Console.Read();
} void ShowNumber(int n)
{
int count = 0;
int x=1;
int y=n/2;
Num = new int[n,n];
if ((n % 2)!=0)
{
Num[y,y] = n*n;
}
for(int i=0;i<4;i++)
{
for(int j=count;j<n-count-1;j++)
{
Num[count,j]=x++;
}
for(int j=count;j<n-count-1;j++)
{
Num[j,n-count-1]=x++;
}
for(int j=n-count-1;j>count;j--)
{
Num[n-count-1,j]=x++;
}
for(int j=n-count-1;j>count;j--)
{
Num[j,count]=x++;
}
count++;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.Console.Write(Num[i,j]+" ");
}
System.Console.Write("\n");
}
}
}
}
{
int[,] ldata;
int ltmp = 0,i,n;
ldata = new int[vNum,vNum];
for(n=1;n <= vNum*2;n++)
{
i=n/2 - n/4;
for(;i<vNum - n/4;i++)
{
ltmp ++;
ldata[(n-1)/4,i] = ltmp;
}
RotateArray(ref ldata);
}
for(n=1;n<=vNum%2 *2;n++)
RotateArray(ref ldata);
for(n=0;n<vNum;n++)
{
for(i=0;i<vNum;i++)
System.Console.Write(ldata[n,i].ToString() + "\t");
System.Console.WriteLine();
}
} private void RotateArray(ref int[,] vData)
{
int[,] ldata;
ldata = new int[vData.GetLength(0),vData.GetLength(1)];
for(int n=0;n<vData.GetLength(0);n++)
for(int i=0;i<vData.GetLength(1);i++)
{
ldata[vData.GetLength(1) - 1 - i,n] = vData[n,i];
}
vData = ldata;
}
{
public FillArrays(int n)
{
this.n = n;
num = new int[n,n];
this.Fill();
}
private int n;
private int x = 0;
private int y = 0;
private int[,] num;
PointTo pt = PointTo.Right ;
public int[,] Result
{
get
{
return num;
}
} private void PointToRight()
{
pt = PointTo.Right;
y++;
x++;
} private void PointToLeft()
{
pt = PointTo.Left;
y--;
x--;
} private void PointToDown()
{
pt = PointTo.Down;
x++;
y--;
} private void PointToUp()
{
pt = PointTo.Up;
x--;
y++;
} private void Fill()
{
for(int i=1; i<= n*n; i++)
{
do
{
if (pt == PointTo.Right)
{
if (y == n)
PointToDown();
else
{
if (num[x,y] == 0)
{
num[x,y] = i;
y++;
break;
}
else
PointToDown();
}
}
if (pt == PointTo.Down)
{
if (x == n)
PointToLeft();
else
{
if (num[x,y] == 0)
{
num[x,y] = i;
x++;
break;
}
else
PointToLeft();
}
}
if (pt == PointTo.Left)
{
if (y < 0)
PointToUp();
else
{
if (num[x,y] == 0)
{
num[x,y] = i;
y--;
break;
}
else
PointToUp();
}
}
if (pt == PointTo.Up)
{
if (x < 0)
PointToRight();
else
{
if (num[x,y] == 0)
{
num[x,y] = i;
x--;
break;
}
else
PointToRight();
}
}
}
while(true);
}
}
}
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static int width;
static int square;
static void Main(string[] args)
{
int n;
while ((n = Convert.ToInt32(Console.ReadLine())) != 0)
{
square = n;
int idx = 1;
width = (n * n).ToString().Length + 1;
Console.Clear();
for (int i = 0; i < (n + 1) / 2; i++)
{
Print(n - i * 2, ref idx);
}
}
} private static void Print(int n, ref int idx)
{
int lt = (square - n) / 2 ;
if (n == 1)
{
Console.SetCursorPosition(lt * width, lt);
Console.Write(idx++);
Console.SetCursorPosition(0, square);
}
for (int i = 0; i < n-1; i++)
{
Console.SetCursorPosition((lt+i)*width, lt);
Console.Write(idx++);
}
for (int i = 0; i < n - 1; i++)
{
Console.SetCursorPosition((lt + n - 1)*width, lt + i);
Console.WriteLine(idx++);
}
for (int i = 0; i < n - 1; i++)
{
Console.SetCursorPosition((lt + n - 1 - i )* width, lt + n - 1);
Console.WriteLine(idx++);
}
for (int i = 0; i < n - 1; i++)
{
Console.SetCursorPosition(lt*width, lt + n - 1 - i);
Console.WriteLine(idx++);
if (n == 2)
Console.SetCursorPosition(0, square);
}
}
}
}