static void Main(string[] args) { int[][] a = new int[4][]; a[0] = new int[4] { 0, 1, 2, 3 }; a[1] = new int[4] { 4, 5, 6, 7 }; a[2] = new int[4] { 8, 9, 10, 11 }; a[3] = new int[4] { 12, 13, 14, 15 }; int[][] flag = new int[4][]; flag[0] = new int[4] {0,0,0,0}; flag[1] = new int[4] {0,0,0,0}; flag[2] = new int[4] {0,0,0,0}; flag[3] = new int[4] {0,0,0,0}; int x = 0; int y = 0; int xSpeed = 1; int ySpeed = 0; int count = 0; /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */ while (count <= 15) { Console.Write(a[y][x] + " "); flag[y][x] = 1; count++; x += xSpeed; y += ySpeed; if (x >3 || x <0 ||y>3||y<0|| flag[y][x] == 1) { if (xSpeed == 1 && ySpeed == 0) { x -= xSpeed; y -= ySpeed; xSpeed = 0; ySpeed = 1; x += xSpeed; y += ySpeed; } else if(xSpeed == 0 && ySpeed == 1) { x -= xSpeed; y -= ySpeed; xSpeed = -1; ySpeed = 0; x += xSpeed; y += ySpeed; } else if (xSpeed == -1 && ySpeed == 0) { x -= xSpeed; y -= ySpeed; xSpeed = 0; ySpeed = -1; x += xSpeed; y += ySpeed; } else if (xSpeed == 0 && ySpeed == -1) { x -= xSpeed; y -= ySpeed; xSpeed = 1; ySpeed = 0; x += xSpeed; y += ySpeed; } }
} Console.Write(a[y][x] + " "); }
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace ConsoleApplication16 { class Program { public enum direction { Right, Down, Left, Up }; static void Main(string[] args) { direction dir = direction.Right; int[,] a = new int[4, 4] { {0, 1 , 2 , 3}, {4, 5 , 6 , 7}, {8 , 9 , 10 , 11}, {12 ,13 , 14 , 15} }; List<int> list = new List<int>(); int i = 0, j = 0; list.Add(a[i, j]); a[i, j] = 9999;//assume 9999 does not exist in the array do { switch (dir) { case direction.Right: if ((j + 1 < 4) && (a[i, j + 1] != 9999)) { j++; list.Add(a[i, j]); a[i, j] = 9999; } else dir = direction.Down; break; case direction.Down: if ((i + 1 < 4) && (a[i + 1, j] != 9999)) { i++; list.Add(a[i, j]); a[i, j] = 9999; } else dir = direction.Left; break; case direction.Left: if ((j - 1 >= 0) && (a[i, j - 1] != 9999)) { j--; list.Add(a[i, j]); a[i, j] = 9999; } else dir = direction.Up; break; case direction.Up: if ((i - 1 >= 0) && (a[i - 1, j] != 9999)) { i--; list.Add(a[i, j]); a[i, j] = 9999; } else dir = direction.Right; break; }; } while (list.Count < 16); foreach (int item in list) Console.WriteLine(item); Console.ReadLine(); } } }
如果按lz的描述,最后5应该不用,到9结束才对。 static void Main(string[] args) { int[,] arr = new int[4, 4]; for (int i = 0; i < arr.GetLength(0); i++) { for (int j = 0; j < arr.GetLength(1); j++) { arr[i, j] = i * arr.GetLength(0) + j; } } int y = arr.GetLength(0), x = arr.GetLength(1), tempX = 0, tempY = 0, count = 0, ring = 1; string direction = "along"; while (tempX < x && tempX >= 0 && tempY < y && tempY >= 0 && count < x * y) { count++; Console.Write(arr[tempY, tempX].ToString().PadLeft(3, ' ')); if (direction == "along") { if (tempX == x - ring) tempY++; else tempX++; if (tempX == x - ring && tempY == y - ring) direction = "Inverse"; } else { if (tempX == ring - 1) tempY--; else tempX--; if (tempX == ring - 1 && tempY == ring) { ring++; direction = "along"; } } } Console.ReadLine(); }
int[,] array={{0,1,2,3}, {4,5,6,7}, {8,9,10,11}, {12,13,14,15}}; int circle = array.Length/2; int n = 4; for (int i = 0; i < circle; i++) { //up for (int j = i; j < n - 1 - i; j++) { Console.Write(array[i, j].ToString("00 "));
} //right for (int k = i; k < n - 1 - i; k++) { Console.Write(array[k, n - i - 1].ToString("00 "));
} //down for (int x = n -1 - i; x > i; x--) { Console.Write(array[n - 1 - i,x].ToString("00 "));
} //left for (int y = n - 1 - i; y > i; y--) { Console.Write(array[y, i].ToString("00 "));
} }
这个好象有问题,换成5行5列的就不行了。只改了这个 int[,] array ={{0,1,2,3,4}, {5,6,7,8,9}, {10,11,12,13,14}, {15,16,17,18,19}}; int circle = array.Length / 2; int n = 5;
int[,] array={{0,1,2,3}, {4,5,6,7}, {8,9,10,11}, {12,13,14,15}}; int circle = array.Length/2; int n = 4; for (int i = 0; i < circle; i++) { //up for (int j = i; j < n - 1 - i; j++) { Console.Write(array[i, j].ToString("00 "));
} //right for (int k = i; k < n - 1 - i; k++) { Console.Write(array[k, n - i - 1].ToString("00 "));
} //down for (int x = n -1 - i; x > i; x--) { Console.Write(array[n - 1 - i,x].ToString("00 "));
} //left for (int y = n - 1 - i; y > i; y--) { Console.Write(array[y, i].ToString("00 "));
{
for()
{
console.write();
}
}
int ySpeed = 0;走 0 1 2 3 走到3的时候,xSpeed = 0;ySpeed = 1;
走到15的时候xSpeed = -1;ySpeed = 0;
依次
int ySpeed = 0;走 0 1 2 3 走到3的时候,xSpeed = 0;ySpeed = 1;
走到15的时候xSpeed = -1;ySpeed = 0;
依次
List<int> list = new List<int>();
for(int i=0;i<4;i++)
{
for(int j =0;j<4;j++)
{
list.Add(num[i][j]);
}
}
你回答都只回答表面的,最主要的就是要怎么循环怎么加。
int x = 0;
int y = 0;
int xSpeed = 1;
int ySpeed = 0;
0 1 2 3从0 到 3,依次访问的是a[0][0],a[0][1],a[0][2],a[0][3];
可以看到y的值不变(a[0][XXXXX], 可以这样访问a[y+ySpeed][x+xSpeed],当然x,y的值要变)走到3的时候越界了,改变方向,依次访问a[1][3],a[2][3],a[3][3];
可以看到x的值不变(a[XXX][3], 在这里的时候xSpeed = 0;ySpeed = 1)走到15,越界,访问14,13,12 xSpeed = -1;ySpeed = 0;
依次
需要一个标记数组。
static void Main(string[] args)
{
int[][] a = new int[4][];
a[0] = new int[4] { 0, 1, 2, 3 };
a[1] = new int[4] { 4, 5, 6, 7 };
a[2] = new int[4] { 8, 9, 10, 11 };
a[3] = new int[4] { 12, 13, 14, 15 }; int[][] flag = new int[4][];
flag[0] = new int[4] {0,0,0,0};
flag[1] = new int[4] {0,0,0,0};
flag[2] = new int[4] {0,0,0,0};
flag[3] = new int[4] {0,0,0,0};
int x = 0;
int y = 0;
int xSpeed = 1;
int ySpeed = 0; int count = 0;
/*
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15 */ while (count <= 15)
{
Console.Write(a[y][x] + " ");
flag[y][x] = 1;
count++;
x += xSpeed;
y += ySpeed; if (x >3 || x <0 ||y>3||y<0|| flag[y][x] == 1)
{
if (xSpeed == 1 && ySpeed == 0)
{
x -= xSpeed;
y -= ySpeed;
xSpeed = 0;
ySpeed = 1;
x += xSpeed;
y += ySpeed;
}
else if(xSpeed == 0 && ySpeed == 1)
{
x -= xSpeed;
y -= ySpeed;
xSpeed = -1;
ySpeed = 0;
x += xSpeed;
y += ySpeed;
}
else if (xSpeed == -1 && ySpeed == 0)
{
x -= xSpeed;
y -= ySpeed;
xSpeed = 0;
ySpeed = -1;
x += xSpeed;
y += ySpeed;
}
else if (xSpeed == 0 && ySpeed == -1)
{
x -= xSpeed;
y -= ySpeed;
xSpeed = 1;
ySpeed = 0;
x += xSpeed;
y += ySpeed;
}
}
} Console.Write(a[y][x] + " ");
}
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication16
{
class Program
{
public enum direction
{
Right,
Down,
Left,
Up
};
static void Main(string[] args)
{
direction dir = direction.Right;
int[,] a = new int[4, 4] {
{0, 1 , 2 , 3},
{4, 5 , 6 , 7},
{8 , 9 , 10 , 11},
{12 ,13 , 14 , 15}
};
List<int> list = new List<int>();
int i = 0, j = 0;
list.Add(a[i, j]);
a[i, j] = 9999;//assume 9999 does not exist in the array
do
{
switch (dir)
{
case direction.Right:
if ((j + 1 < 4) && (a[i, j + 1] != 9999))
{
j++;
list.Add(a[i, j]);
a[i, j] = 9999;
}
else
dir = direction.Down;
break;
case direction.Down:
if ((i + 1 < 4) && (a[i + 1, j] != 9999))
{ i++;
list.Add(a[i, j]);
a[i, j] = 9999;
}
else
dir = direction.Left;
break;
case direction.Left:
if ((j - 1 >= 0) && (a[i, j - 1] != 9999))
{
j--;
list.Add(a[i, j]);
a[i, j] = 9999;
}
else
dir = direction.Up;
break;
case direction.Up:
if ((i - 1 >= 0) && (a[i - 1, j] != 9999))
{
i--;
list.Add(a[i, j]);
a[i, j] = 9999;
}
else
dir = direction.Right;
break;
};
} while (list.Count < 16); foreach (int item in list)
Console.WriteLine(item);
Console.ReadLine();
}
}
}
{
int[,] arr = new int[4, 4];
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
arr[i, j] = i * arr.GetLength(0) + j;
}
} int y = arr.GetLength(0), x = arr.GetLength(1),
tempX = 0, tempY = 0,
count = 0, ring = 1;
string direction = "along";
while (tempX < x && tempX >= 0 && tempY < y && tempY >= 0 && count < x * y)
{
count++;
Console.Write(arr[tempY, tempX].ToString().PadLeft(3, ' '));
if (direction == "along")
{
if (tempX == x - ring)
tempY++;
else
tempX++;
if (tempX == x - ring && tempY == y - ring)
direction = "Inverse";
}
else
{
if (tempX == ring - 1)
tempY--;
else
tempX--;
if (tempX == ring - 1 && tempY == ring)
{
ring++;
direction = "along";
}
}
}
Console.ReadLine();
}
{4,5,6,7},
{8,9,10,11},
{12,13,14,15}};
int circle = array.Length/2;
int n = 4;
for (int i = 0; i < circle; i++)
{
//up
for (int j = i; j < n - 1 - i; j++)
{
Console.Write(array[i, j].ToString("00 "));
}
//right
for (int k = i; k < n - 1 - i; k++)
{
Console.Write(array[k, n - i - 1].ToString("00 "));
}
//down
for (int x = n -1 - i; x > i; x--)
{
Console.Write(array[n - 1 - i,x].ToString("00 "));
}
//left
for (int y = n - 1 - i; y > i; y--)
{
Console.Write(array[y, i].ToString("00 "));
}
}
int[,] array ={{0,1,2,3,4},
{5,6,7,8,9},
{10,11,12,13,14},
{15,16,17,18,19}};
int circle = array.Length / 2;
int n = 5;
{4,5,6,7},
{8,9,10,11},
{12,13,14,15}};
int circle = array.Length/2;
int n = 4;
for (int i = 0; i < circle; i++)
{
//up
for (int j = i; j < n - 1 - i; j++)
{
Console.Write(array[i, j].ToString("00 "));
}
//right
for (int k = i; k < n - 1 - i; k++)
{
Console.Write(array[k, n - i - 1].ToString("00 "));
}
//down
for (int x = n -1 - i; x > i; x--)
{
Console.Write(array[n - 1 - i,x].ToString("00 "));
}
//left
for (int y = n - 1 - i; y > i; y--)
{
Console.Write(array[y, i].ToString("00 "));
}
}
以上只是针对你出的问题,如果任意的话
加上
if(n%2!=0)
{
Console.Write(array[n/2,n/2].ToString("00 "));
}