首页 新闻 会员 周边

根据输入,输出下列矩阵!(C#语言实现)

0
悬赏园豆:20 [已解决问题] 解决于 2013-06-20 15:39

输入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         }
正确答案
冬栀叶的主页 冬栀叶 | 初学一级 | 园豆:186
提问于:2013-06-20 10:56
< >
分享
最佳答案
0

正确的解决办法,虽然有点复杂但是正确的~

 

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();

            }

        }

冬栀叶 | 初学一级 |园豆:186 | 2013-06-20 15:34
其他回答(4)
0

先把矩阵的宽度算出来(n*2+1)

根据宽度创建一个2唯的数组

然后向数组中填入数据.

然后输出数组

收获园豆:5
只会造轮子 | 园豆:2274 (老鸟四级) | 2013-06-20 11:19
0

(2n+1)的平方为右上角。然后逆序输出。没圈做一个递归。

收获园豆:10
在大地画满窗子 | 园豆:102 (初学一级) | 2013-06-20 11:32

请给出详细的code行吗?

支持(0) 反对(0) 冬栀叶 | 园豆:186 (初学一级) | 2013-06-20 15:29
0

非递归算法

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");
                }
            }

        }
    }
}
收获园豆:5
WuRang | 园豆:1730 (小虾三级) | 2013-06-20 11:52

简单运行了你的结果,发现2、3的时候就是不对的

支持(0) 反对(0) 心雨纷扬 | 园豆:309 (菜鸟二级) | 2013-06-20 14:54

@心雨纷扬: 看差了

支持(0) 反对(0) 心雨纷扬 | 园豆:309 (菜鸟二级) | 2013-06-20 15:49
0
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");
            }
        }
心雨纷扬 | 园豆:309 (菜鸟二级) | 2013-06-20 15:49
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册