输入1
输出下面这个矩阵
7 |
8 |
9 |
6 |
1 |
2 |
5 |
4 |
3 |
输入2
输出下面这个矩阵
21 |
22 |
23 |
24 |
25 |
20 |
7 |
8 |
9 |
10 |
19 |
6 |
1 |
2 |
11 |
18 |
5 |
4 |
3 |
12 |
17 |
16 |
15 |
14 |
13 |
输入3
输出下面这个矩阵
43 |
44 |
45 |
46 |
47 |
48 |
49 |
42 |
21 |
22 |
23 |
24 |
25 |
26 |
41 |
20 |
7 |
8 |
9 |
10 |
27 |
40 |
19 |
6 |
1 |
2 |
11 |
28 |
39 |
18 |
5 |
4 |
3 |
12 |
29 |
38 |
17 |
16 |
15 |
14 |
13 |
30 |
37 |
36 |
35 |
34 |
33 |
32 |
31 |
1 private static void PrintGraph(int level, int start) 2 { 3 int[,] arrax = new int[level * 2 + 1, level * 2 + 1]; 4 arrax[level, level] = start; 5 for (int i = 1; i <= level; i++) 6 { 7 //right 8 int y = level + i; 9 int x = level - i; 10 for (int j = 1; j <= 2 * i; j++) 11 { 12 int x1 = x + j; 13 if (j == 1) 14 { 15 arrax[x1, y] = arrax[x1, y - 1] + 1; 16 } 17 else 18 { 19 arrax[x1, y] = arrax[x1 - 1, y] + 1; 20 } 21 } 22 23 //bottom 24 x = level + i; 25 for (int j = 1; j <= 2 * i; j++) 26 { 27 int y1 = y - j; 28 arrax[x, y1] = arrax[x, y1 + 1] + 1; 29 } 30 31 //left 32 y = level - i; 33 34 for (int j = 1; j <= 2 * i; j++) 35 { 36 int x1 = x - j; 37 arrax[x1, y] = arrax[x1 + 1, y] + 1; 38 } 39 40 //top 41 x = level - i; 42 for (int j = 1; j <= 2 * i; j++) 43 { 44 int y1 = y + j; 45 arrax[x, y1] = arrax[x, y1 - 1] + 1; 46 } 47 } 48 49 for (int m = 0; m < level * 2 + 1; m++) 50 { 51 for (int n = 0; n < level * 2 + 1; n++) 52 { 53 Console.Write(string.Format("{0,3}", arrax[m, n])); 54 } 55 Console.WriteLine(); 56 } 57 }
正确的解决办法,虽然有点复杂但是正确的~
staticvoid PrintGraph(int level, int start)
{
int[,] arrax = newint[level * 2 + 1, level * 2 + 1];
arrax[level, level] = start;
for (int i = 1; i <= level; i++)
{
//right
int y = level + i;
int x = level - i;
for (int j = 1; j <= 2 * i; j++)
{
int x1 = x + j;
if (j == 1)
{
arrax[x1, y] = arrax[x1, y - 1] + 1;
}
else
{
arrax[x1, y] = arrax[x1 - 1, y] + 1;
}
}
//bottom
x = level + i;
for (int j = 1; j <= 2 * i; j++)
{
int y1 = y - j;
arrax[x, y1] = arrax[x, y1 + 1] + 1;
}
//left
y = level - i;
for (int j = 1; j <= 2 * i; j++)
{
int x1 = x - j;
arrax[x1, y] = arrax[x1 + 1, y] + 1;
}
//top
x = level - i;
for (int j = 1; j <= 2 * i; j++)
{
int y1 = y + j;
arrax[x, y1] = arrax[x, y1 - 1] + 1;
}
}
for (int m = 0; m < level * 2 + 1; m++)
{
for (int n = 0; n < level * 2 + 1; n++)
{
Console.Write(string.Format("{0,3}", arrax[m, n]));
}
Console.WriteLine();
}
}
先把矩阵的宽度算出来(n*2+1)
根据宽度创建一个2唯的数组
然后向数组中填入数据.
然后输出数组
(2n+1)的平方为右上角。然后逆序输出。没圈做一个递归。
请给出详细的code行吗?
非递归算法
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication6 { class Program { static void Main(string[] args) { int c; bool flag; int[,] rect; int col; int row; int add; int num; int totalCount; int op; //操作次数 int direct; //操作方向 0 右 1下 2左 3上 flag = int.TryParse(Console.ReadLine(), out c); if (flag) { rect = new int[2 * c + 1, 2 * c + 1]; col = c; row = c; add = 1; direct = 0; num = 1; op = 0; rect[row, col] = num; totalCount = (2 * c + 1)*(2*c+1); for (int i = 1; i <= totalCount; i++) { if (op == 2) { add++; op = 0; } switch (direct) { case 0: for (int j = 1; j <= add; j++) { col++; num++; if (num > totalCount) break; rect[row, col] = num; } direct = 1; break; case 1: for (int j = 1; j <= add; j++) { row++; num++; if (num > totalCount) break; rect[row, col] = num; } direct = 2; break; case 2: for (int j = 1; j <= add; j++) { col--; num++; if (num > totalCount) break; rect[row, col] = num; } direct = 3; break; case 3: for (int j = 1; j <= add; j++) { row--; num++; if (num > totalCount) break; rect[row, col] = num; } direct = 0; break; } op++; } for (int i = 0; i < 2 * c + 1; i++ ) { for (int j = 0; j < 2 * c + 1; j++) { Console.Write(rect[i, j] + " "); } Console.Write("\n"); } } } } }
简单运行了你的结果,发现2、3的时候就是不对的
@心雨纷扬: 看差了
private static int maxRowCount = 0; private static int maxColCount = 0; static void Main(string[] args) { int c; bool flag; int[,] rect; int col; int row; int add; int num; int totalCount; int op; //操作次数 int direct; //操作方向 0 右 1下 2左 3上 flag = int.TryParse(Console.ReadLine(), out c); maxRowCount = 2 * c + 1; maxColCount = 2 * c + 1; rect = new int[maxRowCount, maxColCount]; if (flag) { col = c; row = c; direct = 0; num = 1; rect[row, col] = num; totalCount = maxRowCount * maxColCount; var turnTimes = 1;//转向次数 var waitTime = 1;//下一次转向 for (int i = 0; i < totalCount; i++) { if (turnTimes == 3) { turnTimes = 1; waitTime++; } switch (direct) { case 0: for (int j = 0; j < waitTime; j++) { col++; num++; if (num > totalCount) break; rect[row, col] = num;//加1 } Print(rect); direct++; break; case 1: for (int j = 0; j < waitTime; j++) { row++; num++; if (num > totalCount) break; rect[row, col] = num;//加1 } Print(rect); direct++; break; case 2: for (int j = 0; j < waitTime; j++) { col--; num++; if (num > totalCount) break; rect[row, col] = num;//加1 } Print(rect); direct++; break; case 3: for (int j = 0; j < waitTime; j++) { row--; num++; if (num > totalCount) break; rect[row, col] = num;//加1 } Print(rect); direct = 0; break; } turnTimes++; } } } static void Print(int[,] rect) { for (int i = 0; i < maxRowCount; i++) { for (int j = 0; j < maxColCount; j++) { Console.Write(rect[i, j] + "\t"); } Console.Write("\n"); } }