很棘手的问题
将将从1到n*n的n的平方个数,按顺时针方向从小到大排列成阵列,怎么做?如n=4则效果图:
1, 2, 3,4
12,13,14,5
11,16,15,6
10,9, 8, 7
[ 本帖最后由 hanxing15 于 2011-10-28 09:49 编辑 ]
程序代码:以前就有人要问过了。再给你贴一次
Console.WriteLine("Input:");
int n = int.Parse(Console.ReadLine());
int[,] result = new int[n,n];
int no = 1, direction = 0,x = 0,y=0;
for (int i = 0; i < n * n; i++)
{
result[x, y] = no;
if (direction == 0)
{
if (y +1 == n || result[x,y+1]!=0)
{
direction = 1;
x++;
}
else
y++;
}
else if (direction == 1)
{
if (x + 1 == n || result[x+1,y]!= 0)
{
direction = 2;
y--;
}
else
x++;
}
else if (direction == 2)
{
if (y == 0 || result[x, y - 1] != 0)
{
direction = 3;
x--;
}
else
y--;
}
else if (direction == 3)
{
if (result[x - 1, y] != 0)
{
direction = 0;
y++;
}
else
x--;
}
no++;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(result[i, j] + "\t");
}
Console.WriteLine();
}
Console.ReadLine();
程序代码: class Program
{
static int[,] Map;
static int Width;
static int Max;
static void InitMap(int n)
{
Width = n;
Max = n * n;
Map = new int[n, n];
}
static void DrawMap(int i, int j, int k, int val)
{
if (val > Max) return;
Map[i, j] = val++;
switch (k)
{
case 0://向右
j++;
if (j >= Width || Map[i, j] != 0)
{
i += 1;
j -= 1;
k = 1;
}
break;
case 1://向下
i++;
if (i >= Width || Map[i, j] != 0)
{
i -= 1;
j -= 1;
k = 2;
}
break;
case 2://向左
j--;
if (j < 0 || Map[i, j] != 0)
{
i -= 1;
j += 1;
k = 3;
}
break;
case 3://向上
i--;
if (i < 0 || Map[i, j] != 0)
{
i += 1;
j += 1;
k = 0;
}
break;
}
DrawMap(i, j, k, val);
}
static void PrintMap()
{
for (int i = 0; i < Map.GetLength(0); i++)
{
for (int j = 0; j < Map.GetLength(1); j++)
{
Console.Write(Map[i, j] + "\t");
}
Console.Write("\n");
}
}
static void Handler(int n)
{
InitMap(n);
DrawMap(0, 0, 0, 1);
PrintMap();
}
static void Main(string[] args)
{
int n = 1;
string input = "";
while (true)
{
Console.Write("Please input n, 1 <= n <= 10, q to exit:");
input = Console.ReadLine();
if (input == "q") break;
if (Int32.TryParse(input, out n) && n > 0 && n < 11)
Handler(n);
else
Console.WriteLine(input + " is not a right integer.");
}
}
}
程序代码:static void Display(int[,] a)
{
for (int i = 0; i < a.GetLength(0); ++i)
{
for (int j = 0; j < a.GetLength(1); ++j)
{
Console.Write("{0}{1}\t", a[i, j], j < a.GetLength(1) - 1 ? "," : "");
}
Console.WriteLine();
}
}
static int[,] Get(int n)
{
var m = new int[n, n];
Get(m, 1, 0, 0, n);
return m;
}
static void Get(int[,] m, int r, int i, int j, int n)
{
if (n > 0)
{
if (n > 1)
{
for (int x = 0; x < n - 1; ++x)
{
m[i, j + x] = r + x;
m[i + x, j + n - 1] = r + n - 1 + x;
m[i + n - 1, j + 1 + x] = r + 3 * n - 3 - 1 - x;
m[i + 1 + x, j] = r + 4 * n - 4 - 1 - x;
}
Get(m, r + 4 * n - 4, i + 1, j + 1, n - 2);
}
else
{
m[i, j] = r;
}
}
}
[STAThread]
static void Main(string[] args)
{
var m = Get(1);
Display(m);
Console.WriteLine("==========");
m = Get(2);
Display(m);
Console.WriteLine("==========");
m = Get(3);
Display(m);
Console.WriteLine("==========");
m = Get(4);
Display(m);
Console.WriteLine("==========");
m = Get(5);
Display(m);
Console.WriteLine("==========");
m = Get(6);
Display(m);
Console.WriteLine("==========");
Console.ReadLine();
}