【算法比赛】打印有变化规律的数字正方形,看谁的代码扩展性、可读性最好。

时间:2022-10-27 15:23:40
要求:
  打印一组有变化规律的正方形(规则自己分析了)
  如下是宽度为8的情况:
/*
01--02--03--04--05--06--07--08
                             |
28--29--30--31--32--33--34  09
 |                       |   |
27  48--49--50--51--52  35  10
 |   |               |   |   |
26  47  60--61--62  53  36  11
 |   |   |       |   |   |   |
25  46  59  64--63  54  37  12
 |   |   |           |   |   |
24  45  58--57--56--55  38  13
 |   |                   |   |
23  44--43--42--41--40--39  14
 |                           |
22--21--20--19--18--17--16--15
//////////////////////////////
01--02  06--07  15--16  28--29
   /   /   /   /   /   /   /
03  05  08  14  17  27  30  43
 | /   /   /   /   /   /   / |
04  09  13  18  26  31  42  44
   /   /   /   /   /   /   /
10  12  19  25  32  41  45  54
 | /   /   /   /   /   /   / |
11  20  24  33  40  46  53  55
   /   /   /   /   /   /   /
21  23  34  39  47  52  56  61
 | /   /   /   /   /   /   / |
22  35  38  48  51  57  60  62
   /   /   /   /   /   /   /
36--37  49--50  58--59  63--64
//////////////////////////////
01--02  04  07  11  16  22  29
   />  /   /   /   /   /   /
03  05  08  12  17  23  30  37
   />  /   /   /   /   /   /
06  09  13  18  24  31  38  44
   />  /   /   /   /   /   /
10  14  19  25  32  39  45  50
   />  /   /   /   /   /   /
15  20  26  33  40  46  51  55
   />  /   /   /   /   /   /
21  27  34  41  47  52  56  59
   />  /   /   /   /   /   /
28  35  42  48  53  57  60  62
   />  />  />  />  />  />  /
36  43  49  54  58  61  63--64
//////////////////////////////
01  09  23  35  45  53  59  63
   \   \   \   \   \   \   \>
16  02  10  24  36  46  54  60
   \   \   \   \   \   \   \>
29  17  03  11  25  37  47  55
   \   \   \   \   \   \   \>
40  30  18  04  12  26  38  48
   \   \   \   \   \   \   \>
49  41  31  19  05  13  27  39
   \   \   \   \   \   \   \>
56  50  42  32  20  06  14  28
   \   \   \   \   \   \   \>
61  57  51  43  33  21  07  15
   \>  \>  \>  \>  \>  \>  \>
64  62  58  52  44  34  22  08
//////////////////////////////
64--63--62--61--60--59--58--57
                             |
37--38--39--40--41--42--43  56
 |                       |   |
36--35--34--33--32--31  44  55
                     |   |   |
17--18--19--20--21  30  45  54
 |               |   |   |   |
16--15--14--13  22  29  46  53
             |   |   |   |   |
05--06--07  12  23  28  47  52
 |       |   |   |   |   |   |
04--03  08  11  24  27  48  51
     |   |   |   |   |   |   |
01--02  09--10  25--26  49--50
//////////////////////////////
01--02--03--04--05--06--07--08
                             |
16--15--14--13--12--11--10--09
 |                       
17--18--19--20--21--22--23--24
                             |
32--31--30--29--28--27--26--25
 |
33--34--35--36--37--38--39--40
                             |
48--47--46--45--44--43--42--41
 |
49--50--51--52--53--54--55--56
                             |
64--63--62--61--60--59--58--57
*/


输入:正方形的宽度Size(整数,范围2-9)
输出:一组宽度为Size的正方形

输入样例:8
输出样例:
01  02  03  04  05  06  07  08
                              
28  29  30  31  32  33  34  09
                              
27  48  49  50  51  52  35  10
                              
26  47  60  61  62  53  36  11
                              
25  46  59  64  63  54  37  12
                              
24  45  58  57  56  55  38  13
                              
23  44  43  42  41  40  39  14
                              
22  21  20  19  18  17  16  15
-------
01  02  06  07  15  16  28  29
                            
03  05  08  14  17  27  30  43
                              
04  09  13  18  26  31  42  44
                            
10  12  19  25  32  41  45  54
                              
11  20  24  33  40  46  53  55
                            
21  23  34  39  47  52  56  61
                              
22  35  38  48  51  57  60  62
                            
36  37  49  50  58  59  63  64
-------
01  02  04  07  11  16  22  29
                            
03  05  08  12  17  23  30  37
                            
06  09  13  18  24  31  38  44
                            
10  14  19  25  32  39  45  50
                            
15  20  26  33  40  46  51  55
                            
21  27  34  41  47  52  56  59
                            
28  35  42  48  53  57  60  62
                            
36  43  49  54  58  61  63  64
-------
01  09  23  35  45  53  59  63
                             
16  02  10  24  36  46  54  60
                             
29  17  03  11  25  37  47  55
                             
40  30  18  04  12  26  38  48
                             
49  41  31  19  05  13  27  39
                             
56  50  42  32  20  06  14  28
                             
61  57  51  43  33  21  07  15
                             
64  62  58  52  44  34  22  08
-------
64  63  62  61  60  59  58  57
                              
37  38  39  40  41  42  43  56
                              
36  35  34  33  32  31  44  55
                              
17  18  19  20  21  30  45  54
                              
16  15  14  13  22  29  46  53
                              
05  06  07  12  23  28  47  52
                              
04  03  08  11  24  27  48  51
                              
01  02  09  10  25  26  49  50
-------
01  02  03  04  05  06  07  08
                              
16  15  14  13  12  11  10  09
                         
17  18  19  20  21  22  23  24
                              
32  31  30  29  28  27  26  25
  
33  34  35  36  37  38  39  40
                              
48  47  46  45  44  43  42  41
  
49  50  51  52  53  54  55  56
                              
64  63  62  61  60  59  58  57


提供完整的控制台程序,扩展性、可读性最好的奖励200分,其他酌情散掉。

269 个解决方案

#1


有难度   先顶下再说

#2


收藏

#3


看上去很难啊。 帮你顶啊!

#4


kankan

#5


还有哈..
顶...

#6


楼主的题太难了,估计这次一倒一大片了。

BTW,最近楼主分是不是多的溢出了 n_n

#7


mark先~

#8


又来了 上次作业还没交呢

#9


难度有点大!!

#10


嗯,可以酌情散掉了

#11


引用 6 楼 nattystyle 的回复:
楼主的题太难了,估计这次一倒一大片了。

BTW,最近楼主分是不是多的溢出了 n_n
那确实。。。还有30000多分不知道怎么花。。。[img=http://p.blog.csdn.net/images/p_blog_csdn_net/zswangii/%E6%8C%96%E9%BC%BB%E5%AD%94.gif]图[/img]

#12


题目好多啊
就做第一题吧


        private const int WIDTH = 8;
        private int Min(params int[] vars) // 取最小值
        { 
            //略
        } 

        // 第几圈
        private int GetCircle(Point p)
        {
            return Min(p.X, p.Y, WIDTH - p.X, WIDTH - p.Y);
        }

        // 方向
        private int GetDirection(Point p)
        {
            switch (GetCircle(p))
            {
                case p.Y: return 1;
                case p.X: return 2;
                case WIDTH - p.Y: return 3;
                case WIDTH - p.X: return 4;
            }
        }

        // 顺序
        private int GetSequence(Point p)
        {
            switch (GetDirection(p))
            {
                case 1: return p.X - GetCircle(p) + 1;
                case 2: return p.Y - GetCircle(p) + 1;
                case 3: return WIDTH - GetCircle(p) + 1 - p.X;
                case 4: return WIDTH - GetCircle(p) + 1 - p.Y;
            }
        }

        private int GetValue(Point p)
        {
            int Value = 0;

            for(int i=1;i<GetCircle(p);i++)
            {
                Value += 4 * (WIDTH - 1 - i * 2);
            }

            Value += (GetDirection(p) - 1) * (WIDTH - 1 - (GetCircle(p) - 1) * 2) + GetSequence(p);

            return Value;
        }

#13


我也简单实现了一下第一题:
using System;

public class ShowData
{
    public static void Main( string[] args )
    {
        if(args.Length <= 0){
            Console.WriteLine("至少输入一个数字");
            return;
        }
        // 获取输入的行和列
        int x,y;
        try{
            x = int.Parse(args[0]);
            if(args.Length > 1)
                y = int.Parse(args[1]);
            else
                y = x;
        }catch{
            Console.WriteLine("请输入数字");
            return;
        }
        
        int[,] arr = new int[x,y];
        InsertArr(arr, 1, 0);
        OutPut(arr);
    }
    
    private static void InsertArr(int[,] arr, int level, int numBegin){
        int x = arr.GetLength(0);
        int y = arr.GetLength(1);

        // 判断是否最内圈
        if(numBegin >= (x * y))
            return;
        
        // 第一行
        for(int i=level-1;i<=y-level;i++){
            numBegin++;
            arr[level-1,i] = numBegin;
        }
        // 中间行的最后一列
        for(int i=level;i<x-level;i++){
            numBegin++;
            arr[i,y-level] = numBegin;
        }
        // 最后一行
        for(int i=y-level;i>=level-1;i--){
            numBegin++;
            arr[x-level,i] = numBegin;
        }
        // 中间行的第一列
        for(int i=x-level-1;i>=level;i--){
            numBegin++;
            arr[i,level-1] = numBegin;
        }
        
        // 递归
        InsertArr(arr, level+1, numBegin);
    }
    
    private static void OutPut(int[,] arr){
        for(int i=0;i<arr.GetLength(0);i++){
            for(int j=0;j<arr.GetLength(1);j++){
                Console.Write(arr[i,j].ToString("00") + ",");
            }
            Console.WriteLine();
        }
    }
}

#14


挖塞!
MARK

#15


比较烦琐

#16


注:
说明一下,我的程序扩展了一下题目,可以输入一个参数,也可以输入2个参数
如果只输入一个参数,那么输出就是一个正方形

如果输入2个参数,那么输出就是一个长方形

#17


看就好了 写这个东西还是C++好用

#18



using System;
namespace PrintBox
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("请输入2--9的整数:");
            string input = Console.ReadLine();
            int row = 0;
            while (!int.TryParse(input, out row) || (row > 9 || row < 2))
            {
                Console.Write("请输入2--9的整数:");
                input = Console.ReadLine();
            }
            PrintBox(row);
            Console.ReadKey();
        }
        public static void PrintBox(int row)
        {
            int[,] array = new int[row, row];
            int index = 1;
            int j = 0, k = 0;

            #region first
            int right = row - 1, left = 0, bottom = row - 1, top = 1;
            for (int i = 0; i < row / 2 + row % 2; i++)
            {
                while (k <= right)
                    array[j, k++] = index++;
                k--;
                j++;
                while (j <= bottom)
                    array[j++, k] = index++;
                j--;
                k--;
                while (k >= left)
                    array[j, k--] = index++;
                k++;
                j--;
                while (j >= top)
                    array[j--, k] = index++;
                j++;
                k++;
                right--;
                left++;
                bottom--;
                top++;
            }
            for (int a = 0; a < row; a++)
            {
                for (int b = 0; b < row; b++)
                    Console.Write(array[a, b].ToString().PadLeft(2, '0') + " ");
                Console.WriteLine();
            }
            Console.WriteLine("-----");
            #endregion

            #region second
            index = 1;
            j = k = 0;
            for (int i = 0; i < 2 * row - 1; i++)
            {
                while (j + k == i)
                {
                    array[j, k] = index++;
                    if (j == 0 && k == 0)
                    {
                        k++;
                        continue;
                    }
                    else
                    {
                        if (j == 0)
                        {
                            while (j != i)
                            {
                                j++;
                                k--;
                                array[j, k] = index++;
                            }
                            if (j != row - 1)
                                j++;
                            else
                                k++;
                            break;
                        }
                        if (j == row - 1)
                        {
                            while (k != row - 1)
                            {
                                j--;
                                k++;
                                array[j, k] = index++;
                            }
                            j++;
                            break;
                        }
                        //k++;
                        if (k == 0)
                        {
                            while (k != i)
                            {
                                j--;
                                k++;
                                array[j, k] = index++;
                            }
                            if (k != row - 1)
                                k++;
                            else
                                j++;
                            break;
                        }
                        if (k == row - 1)
                        {
                            while (j != row - 1)
                            {
                                j++;
                                k--;
                                array[j, k] = index++;
                            }
                            k++;
                            break;
                        }
                        //j++;
                    }
                }
            }

            for (int a = 0; a < row; a++)
            {
                for (int b = 0; b < row; b++)
                    Console.Write(array[a, b].ToString().PadLeft(2, '0') + " ");
                Console.WriteLine();
            }
            Console.WriteLine("-----");
            #endregion

            #region third
            index = 1;
            j = k = 0;
            for (int i = 0; i < 2 * row - 1; i++)
            {
                while (j + k == i)
                {
                    array[j, k] = index++;
                    if (j == 0 && k == 0)
                    {
                        k++;
                        continue;
                    }
                    else
                    {
                        if (j == 0)
                        {
                            while (j != i)
                            {
                                j++;
                                k--;
                                array[j, k] = index++;
                            }
                            if (j == row - 1 && k == 0)
                            {
                                j = k + 1;
                                k = row - 1;
                                break;
                            }
                            if (j != row - 1)
                            {
                                k = j + 1;
                                j = 0;
                            }
                        }
                        if (k == row - 1 && j != 0)
                        {
                            while (j != row - 1)
                            {
                                j++;
                                k--;
                                array[j, k] = index++;
                            }
                            if (k == row - 1 && j == row - 1)
                                break;
                            if (k != row - 1)
                            {
                                j = k + 1;
                                k = row - 1;
                            }
                        }
                    }
                }
            }

            for (int a = 0; a < row; a++)
            {
                for (int b = 0; b < row; b++)
                    Console.Write(array[a, b].ToString().PadLeft(2, '0') + " ");
                Console.WriteLine();
            }
            Console.WriteLine("-----");

            #endregion

#19


接上面

            #region fourth
            index = 1;
            j = k = 0;
            for (int i = 0; i < row; i++)
            {
                j = 0;
                k = i;
                for (j = 0; k < row; )
                {
                    array[j, k] = index++;
                    j++;
                    k++;
                }
                if (i != 0)
                {
                    j = i;
                    k = 0;
                    for (k = 0; j < row; )
                    {
                        array[j, k] = index++;
                        j++;
                        k++;
                    }
                }
            }

            for (int a = 0; a < row; a++)
            {
                for (int b = 0; b < row; b++)
                    Console.Write(array[a, b].ToString().PadLeft(2, '0') + " ");
                Console.WriteLine();
            }
            Console.WriteLine("-----");

            #endregion

            #region fifth
            index = row * row;
            j = k = 0;
            right = row - 1;
            left = 0;
            bottom = row - 1;
            top = 1;
            for (int i = 0; i < row / 2 + row % 2; i++)
            {
                while (k <= right)
                    array[j, k++] = index--;
                j++;
                k--;
                while (j <= bottom)
                    array[j++, k] = index--;
                k--;
                j--;
                while (j >= top)
                    array[j--, k] = index--;
                k--;
                j++;
                while (k >= left)
                    array[j, k--] = index--;
                j++;
                k++;
                right -= 2;
                top += 2;
            }
            for (int a = 0; a < row; a++)
            {
                for (int b = 0; b < row; b++)
                    Console.Write(array[a, b].ToString().PadLeft(2, '0') + " ");
                Console.WriteLine();
            }
            Console.WriteLine("-----");

            #endregion

            #region sixth
            index = 1;
            j = k = 0;
            for (int i = 0; i < row; i++)
            {
                if (k == 0)
                {
                    for (; k < row; )
                    {
                        array[j, k] = index++;
                        k++;
                    }
                    j++;
                    if (k == row)
                        k--;
                    continue;
                }
                if (k == row - 1)
                {
                    for (; k >= 0; )
                    {
                        array[j, k] = index++;
                        k--;
                    }
                    j++;
                    if (k < 0)
                        k++;
                    continue;
                }
            }

            for (int a = 0; a < row; a++)
            {
                for (int b = 0; b < row; b++)
                    Console.Write(array[a, b].ToString().PadLeft(2, '0') + " ");
                Console.WriteLine();
            }
            Console.WriteLine("-----");
            #endregion
        }

    }
}

#20


6个图形在一个程序中输出吗?

#21


输出“00”可以这样:
Console.Write("{0}{1:D2}", i > 0 ? " " : "", Numbers[i, j]);

#22


引用 20 楼 wdgphc 的回复:
6个图形在一个程序中输出吗?
是的。

#23


这次要求可读性了啊这次. 中午吃饭的时候想想看看.

#24


不错

#25


收藏一下 有空做

#26


纯粹为了接分而来!

#27


帅气 就喜欢这样的题
今天活忙  有时间研究下
感觉不难实现
俺是学数学的!
为数学骄傲

#28


很牛????
要学习学习

#29


先收藏了,很好的学习和锻炼的题目!!

#30


学习,收藏

#31


时间来不及了,先做了2个.

        private void p1(int n)
        {
            int[,] array = new int[n, n];
            array[0, 0] = 1;
            int gridx = 0, gridy = 0, forward = 6;
            for (int i = 2; i <= n * n; i++)
            {
                switch (forward)
                {
                    case 2:
                        if ((gridy - 1 >= 0) && (array[gridx, gridy - 1] == 0))
                            array[gridx, --gridy] = i;
                        else
                        {
                            array[++gridx, gridy] = i;
                            forward = 6;
                        }
                        break;
                    case 4:
                        if ((gridx - 1 >= 0) && (array[gridx - 1, gridy] == 0))
                            array[--gridx, gridy] = i;
                        else
                        {
                            array[gridx, --gridy] = i;
                            forward = 2;
                        }
                        break;
                    case 6:
                        if ((gridx + 1 < n) && (array[gridx + 1, gridy] == 0))
                            array[++gridx, gridy] = i;
                        else
                        {
                            array[gridx, ++gridy] = i;
                            forward = 8;
                        }
                        break;
                    case 8:
                        if ((gridy + 1 < n) && (array[gridx, gridy + 1] == 0))
                            array[gridx, ++gridy] = i;
                        else
                        {
                            array[--gridx, gridy] = i;
                            forward = 4;
                        }
                        break;
                }
            }
            printArray(array, n);
        }

        private void p2(int n)
        {
            int[,] array = new int[n, n];
            array[0, 0] = 1; array[1, 0] = 2;
            int gridx = 1, gridy = 0, forward = 7;
            for (int i = 3; i <= n * n; i++)
            {
                switch (forward)
                {
                    case 3:
                        if ((gridx + 1 < n) && (gridy - 1 >= 0) && (array[gridx + 1, gridy - 1] == 0))
                            array[++gridx, --gridy] = i;
                        else
                        {
                            if (gridx + 1 < n)
                                array[++gridx, gridy] = i;
                            else
                                array[gridx, ++gridy] = i;
                            forward = 7;
                        }
                        break;
                    case 7:
                        if ((gridx - 1 >= 0) && (gridy + 1 < n) && (array[gridx - 1, gridy + 1] == 0))
                            array[--gridx, ++gridy] = i;
                        else
                        {
                            if (gridy + 1 < n)
                                array[gridx, ++gridy] = i;
                            else
                                array[++gridx, gridy] = i;
                            forward = 3;
                        }
                        break;
                }
            }
            printArray(array, n);
        }

        private void printArray(int[,] array, int num)
        {
            for (int i = 0; i < num; i++)
            {
                for (int j = 0; j < num; j++)
                    Console.Write(array[j, i].ToString("00") + " ");
                Console.WriteLine();
            }
        }


调用
p1(8);
p2(8);

#32


using System;
namespace PrintArray
{
    class PrintArray
    {
        static private void p1(int n)
        {
            int[,] array = new int[n, n];
            array[0, 0] = 1;
            int gridx = 0, gridy = 0, forward = 6;
            for (int i = 2; i <= n * n; i++)
            {
                switch (forward)
                {
                    case 2:
                        if ((gridy - 1 >= 0) && (array[gridx, gridy - 1] == 0))
                            array[gridx, --gridy] = i;
                        else
                        {
                            array[++gridx, gridy] = i;
                            forward = 6;
                        }
                        break;
                    case 4:
                        if ((gridx - 1 >= 0) && (array[gridx - 1, gridy] == 0))
                            array[--gridx, gridy] = i;
                        else
                        {
                            array[gridx, --gridy] = i;
                            forward = 2;
                        }
                        break;
                    case 6:
                        if ((gridx + 1 < n) && (array[gridx + 1, gridy] == 0))
                            array[++gridx, gridy] = i;
                        else
                        {
                            array[gridx, ++gridy] = i;
                            forward = 8;
                        }
                        break;
                    case 8:
                        if ((gridy + 1 < n) && (array[gridx, gridy + 1] == 0))
                            array[gridx, ++gridy] = i;
                        else
                        {
                            array[--gridx, gridy] = i;
                            forward = 4;
                        }
                        break;
                }
            }
            printArray(array, n);
        }
        static private void p2(int n)
        {
            int[,] array = new int[n, n];
            array[0, 0] = 1; array[1, 0] = 2;
            int gridx = 1, gridy = 0, forward = 7;
            for (int i = 3; i <= n * n; i++)
            {
                switch (forward)
                {
                    case 3:
                        if ((gridx + 1 < n) && (gridy - 1 >= 0))
                            array[++gridx, --gridy] = i;
                        else
                        {
                            if (gridx + 1 < n)
                                array[++gridx, gridy] = i;
                            else
                                array[gridx, ++gridy] = i;
                            forward = 7;
                        }
                        break;
                    case 7:
                        if ((gridx - 1 >= 0) && (gridy + 1 < n))
                            array[--gridx, ++gridy] = i;
                        else
                        {
                            if (gridy + 1 < n)
                                array[gridx, ++gridy] = i;
                            else
                                array[++gridx, gridy] = i;
                            forward = 3;
                        }
                        break;
                }
            }
            printArray(array, n);
        }
        static private void p3(int n)
        {
            int[,] array = new int[n, n];
            array[0, 0] = 1;
            int gridx = 0, gridy = 0;
            for (int i = 2; i <= n * n; i++)
            {
                if ((gridx - 1 >= 0) && (gridy + 1 < n))
                    array[--gridx, ++gridy] = i;
                else
                {
                    if (gridy + 1 < n)
                        array[gridx = gridy + 1, gridy = 0] = i;
                    else
                    {
                        gridy = gridx + 1;
                        gridx = n - 1;
                        array[gridx, gridy] = i;
                    }
                }
            }
            printArray(array, n);
        }
        static private void p4(int n)
        {
            int[,] array = new int[n, n];
            array[0, 0] = 1;
            int gridx = 0, gridy = 0;
            for (int i = 2; i <= n * n; i++)
            {
                if ((gridx + 1 < n) && (gridy + 1 < n))
                    array[++gridx, ++gridy] = i;
                else
                {
                    if (gridy < gridx)
                    {
                        array[gridx = 0, gridy = n - gridy - 1] = i;
                    }
                    else
                    {
                        array[gridx = n - gridx, gridy = 0] = i;
                    }
                }
            }
            printArray(array, n);
        }
        static private void p5(int n)
        {
            int[,] array = new int[n, n];
            array[0, n - 1] = 1;
            int gridx = 0, gridy = n - 1, forward = 8;
            for (int i = 2; i <= n * n; i++)
            {
                switch (forward)
                {
                    case 2:
                        array[gridx, --gridy] = i;
                        if (gridx + gridy + 1 == n)
                        {
                            forward = 4;
                        }
                        break;
                    case 4:
                        if (gridx - 1 >= 0)
                            array[--gridx, gridy] = i;
                        else
                        {
                            array[gridx, --gridy] = i;
                            forward = 6;
                        }
                        break;
                    case 6:
                        array[++gridx, gridy] = i;
                        if (gridx + gridy + 1 == n)
                        {
                            forward = 8;
                        }
                        break;
                    case 8:
                        if (gridy + 1 < n)
                            array[gridx, ++gridy] = i;
                        else
                        {
                            array[++gridx, gridy] = i;
                            forward = 2;
                        }
                        break;
                }
            }
            printArray(array, n);
        }
        static private void p6(int n)
        {
            int[,] array = new int[n, n];
            array[0, 0] = 1;
            int gridx = 0, gridy = 0, forward = 6;
            for (int i = 2; i <= n * n; i++)
            {
                switch (forward)
                {
                    case 4:
                        if (gridx - 1 >= 0)
                            array[--gridx, gridy] = i;
                        else
                        {
                            array[gridx, ++gridy] = i;
                            forward = 6;
                        }
                        break;
                    case 6:
                        if (gridx + 1 < n)
                            array[++gridx, gridy] = i;
                        else
                        {
                            array[gridx, ++gridy] = i;
                            forward = 4;
                        }
                        break;
                }
            }
            printArray(array, n);
        }
        static private void printArray(int[,] array, int num)
        {
            for (int i = 0; i < num; i++)
            {
                for (int j = 0; j < num; j++)
                    Console.Write(array[j, i].ToString("00") + " ");
                Console.WriteLine();
            }
            Console.WriteLine("-----------------------");
        }
    }
}

#33


只能输入8000个字符啊,只好把Main函数扔到这楼了.

static void Main()
        {
            char c = '0';
            while (c != 'n' && c != 'N')
            {
                Console.Write("输入2-9:");
                c = Console.ReadKey().KeyChar;
                Console.WriteLine();
                if (c < '2' || c > '9') continue;
                int n = c - '0';
                p1(n);
                p2(n);
                p3(n);
                p4(n);
                p5(n);
                p6(n);
            }
        }


Mian函数放在32楼的Class中 为完整代码 --- wdghc . 时间仓促,不足请指教.

#34


总觉得应该有更好办法的  而不是一个个输出来

#35


这个题真的很考人的逻辑思维, 有时间来  做一下!!!!!

#36


引用 8 楼 LQknife 的回复:
又来了 上次作业还没交呢 

#37


感谢charley450、ojlovecd、wdgphc参与

目前还没有出现更精彩的代码,加100继续期待。

#38


楼主分多的溢水了汗死了。

#39


又散分了?jfjf
每天接分,提高知名度

#40


引用 37 楼 zswang 的回复:
感谢charley450、ojlovecd、wdgphc参与 

目前还没有出现更精彩的代码,加100继续期待。


呵呵,我一般都是能实现为主,请给个努力的方向-> 什么方式为精彩完美?

#41


我总共才200多分,所以不懂也要帮顶

#42


只写一个可以吗?
写第二个吧


using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int len = 0;
            if (int.TryParse(Console.ReadLine().Substring(0, 1),out len))
                output(matrix(len));
            Console.ReadLine();
        }

        static int[,] matrix(int len)
        {
            int num = 1;
            int[,] matrix = new int[len, len];

            int n = 2 * (len - 1);

            int x = 0;
            for (int i = 0; i <= 2 * len - 1; i++)
            {
                if (i < len)
                    x = 0;
                else
                    x = i - len + 1;
                while (x <= i && x < len && i - x < len)
                {
                    if (i % 2 == 1)
                    {
                        matrix[x, i - x] = num;
                    }
                    else
                    {
                        matrix[i - x, x] = num;
                    }
                    x++;
                    num++;
                }
            }

            return matrix;
        }

        static void output(int[,] matrix)
        {
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    Console.Write(matrix[i, j].ToString().PadLeft(2, '0') + "   ");
                }
                Console.WriteLine();
            }
        }
    }
}

#43


引用 40 楼 wdgphc 的回复:
呵呵,我一般都是能实现为主,请给个努力的方向-> 什么方式为精彩完美?
呵呵,如果有新的图像加入现在的代码又的重新构建一遍,总得来说就是代码重复利用率不高。
可以考虑采用面向对象的方法。[img=http://p.blog.csdn.net/images/p_blog_csdn_net/renrousousuo/49.gif]图[/img]

#44


除了最后一题目和第1题,
其他都超级难啊!

#45


关注!

#46


继续关注!

#47


花了几个小时,6个图都比较圆满地解决了.
可读行和扩展性 马马虎虎,呵呵.不废话,上code.

using System;
using System.Collections;
using System.Collections.Generic;

namespace PrintNum2
{
    enum 方向 { 向右=1,向下,向左,向上,向左下,向右上,向右下,向左上,无 };
    struct Point
    {
        public int x;
        public int y;
        public Point(int _x, int _y)
        { 
            x=_x;
            y=_y;
        }
    }
    class  ChangeInfo
    {
        public Point        pt原点= new Point (0,0);//起始点坐标
        public int          i原点值=1;//起始点值
        public int          i变化值=1;//变化值 1或-1
        public 方向         起始方向 = 方向.无;
        public Dictionary<方向, List<方向>> dic固定转换 = new Dictionary<方向, List<方向>>();
        public List<Point>  ls离散起始点 = new List<Point> ();
    }
    class csPrintNum
    {
        bool b找到下一点坐标 = false;
        int[,] matrix = null;
        int x;
        int y;
        int v;
        int n;
        方向 last方向 = 方向.无;
        public csPrintNum(int _n, ChangeInfo _info)
        {
            n = _n;
            int iCount = n * n;
            matrix = new int[n, n];
            x = _info.pt原点.x;
            y = _info.pt原点.y;
            v = _info.i原点值;
            matrix[x, y] = v;
            v += _info.i变化值;

            for (int i = 1; i < iCount; i++)//灌入数据_n*_n次到matrix
            {  
                if(i==1)
                     Move (_info .起始方向 );
                if (!b找到下一点坐标 && _info .dic固定转换.ContainsKey(last方向))
                {
                    List<方向> ls= _info .dic固定转换 [last方向 ] as List <方向>;
                    for (int k = 0;!b找到下一点坐标 &&  k < ls.Count; k++)
                    {
                        Move(ls[k]);
                    }
                }  
                //从离散点找下个坐标
                if (!b找到下一点坐标 && _info.ls离散起始点.Count > 0)
                {
                    x = _info.ls离散起始点[0].x;
                    y = _info.ls离散起始点[0].y;
                    last方向 = 方向.无;
                    _info.ls离散起始点.RemoveAt(0);
                }
                matrix[x, y] = v;
                v += _info.i变化值;
                b找到下一点坐标 = false;
            }
        }
        bool Move(方向 _方向)
        {
            if (_方向 == 方向.向右 && x < n - 1 && matrix[x + 1, y] == 0)
            {
                x += 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            else if (_方向 == 方向.向左 && x > 0 && matrix[x - 1, y] == 0)
            {
                x -= 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            else if (_方向 == 方向.向上 && y > 0 && matrix[x, y - 1] == 0)
            {
                y -= 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            else if (_方向 == 方向.向下 && y < n - 1 && matrix[x, y + 1] == 0)
            {
                y += 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            else if (_方向 == 方向.向左下 && x > 0 && y < n -1  && matrix[ x -1, y + 1] == 0)
            {
                x -= 1;
                y += 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            else if (_方向 == 方向.向右上 && x < n-1 && y > 0  && matrix[ x + 1, y - 1] == 0)
            {
                x += 1;
                y -= 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            else if (_方向 == 方向.向右下 && x < n - 1 && y < n-1 && matrix[x + 1, y + 1] == 0)
            {
                x += 1;
                y += 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            return false;
        }
        public void PrintMatrix()
        {
            if (matrix != null && matrix.Rank == 2)
            {
                for (int i = 0; i <= matrix.GetUpperBound(0); i++)
                {
                    for (int j = 0; j <= matrix.GetUpperBound(1); j++)
                    {
                        Console.Write(matrix[j, i].ToString("d2") + " ");
                    }
                    Console.WriteLine();
                }
                Console.WriteLine("-----------");
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int n=8;
            ChangeInfo info_1 = new ChangeInfo();
            info_1.起始方向 = 方向.向右;
            info_1.dic固定转换.Add(方向.向右, new List<方向>(new 方向[] { 方向.向右, 方向.向下 }));
            info_1.dic固定转换.Add(方向.向下, new List<方向>(new 方向[] { 方向.向下, 方向.向左 }));
            info_1.dic固定转换.Add(方向.向左, new List<方向>(new 方向[] { 方向.向左, 方向.向上 }));
            info_1.dic固定转换.Add(方向.向上, new List<方向>(new 方向[] { 方向.向上, 方向.向右 }));

            csPrintNum cs1 = new csPrintNum(n, info_1);
            cs1.PrintMatrix();

            ChangeInfo info_2 = new ChangeInfo();
            info_2.起始方向 = 方向.向右;
            info_2.dic固定转换.Add(方向.向右, new List<方向>(new 方向[] { 方向.向左下, 方向.向右上 }));
            info_2.dic固定转换.Add(方向.向下, new List<方向>(new 方向[] { 方向.向右上, 方向.向左下 }));
            info_2.dic固定转换.Add(方向.向左下, new List<方向>(new 方向[] { 方向.向左下, 方向.向下, 方向.向右 }));
            info_2.dic固定转换.Add(方向.向右上, new List<方向>(new 方向[] { 方向.向右上, 方向.向右, 方向.向下 }));
            csPrintNum cs2 = new csPrintNum(8, info_2);
            cs2.PrintMatrix();

            ChangeInfo info_3 = new ChangeInfo();
            info_3.dic固定转换.Add(方向.无, new List<方向>(new 方向[] { 方向.向左下 }));
            info_3.dic固定转换.Add(方向.向左下, new List<方向>(new 方向[] { 方向.向左下 }));
            for (int i = 1; i < n; i++)
                info_3.ls离散起始点.Add(new Point(i, 0));
            for (int i = 1; i < n; i++)
                info_3.ls离散起始点.Add(new Point(n - 1, i));
            csPrintNum cs3 = new csPrintNum(n, info_3);
            cs3.PrintMatrix();

            ChangeInfo info_4 = new ChangeInfo();
            info_4.dic固定转换.Add(方向.无, new List<方向>(new 方向[] { 方向.向右下 }));
            info_4.dic固定转换.Add(方向.向右下, new List<方向>(new 方向[] { 方向.向右下 }));
            for (int i = 1; i < n; i++)
            {
                info_4.ls离散起始点.Add(new Point(i, 0));
                info_4.ls离散起始点.Add(new Point(0, i));
            }
            csPrintNum cs4 = new csPrintNum(n, info_4);
            cs4.PrintMatrix();

            ChangeInfo info_5 = new ChangeInfo();
            info_5.i原点值 = 64;
            info_5.i变化值 = -1;
            info_5.起始方向 = 方向.向右;
            info_5.dic固定转换.Add(方向.向右, new List<方向>(new 方向[] { 方向.向右, 方向.向下 }));
            info_5.dic固定转换.Add(方向.向下, new List<方向>(new 方向[] { 方向.向右, 方向.向下, 方向.向左 }));
            info_5.dic固定转换.Add(方向.向左, new List<方向>(new 方向[] { 方向.向上, 方向.向左, 方向.向下 }));
            info_5.dic固定转换.Add(方向.向上, new List<方向>(new 方向[] { 方向.向上, 方向.向左 }));
            csPrintNum cs5 = new csPrintNum(n, info_5);
            cs5.PrintMatrix();

            ChangeInfo info_6 = new ChangeInfo();
            info_6.起始方向 = 方向.向右;
            info_6.dic固定转换.Add(方向.向右, new List<方向>(new 方向[] { 方向.向右, 方向.向下 }));
            info_6.dic固定转换.Add(方向.向下, new List<方向>(new 方向[] { 方向.向右, 方向.向左 }));
            info_6.dic固定转换.Add(方向.向左, new List<方向>(new 方向[] { 方向.向左, 方向.向下 }));
            csPrintNum cs6 = new csPrintNum(n, info_6);
            cs6.PrintMatrix();
        }
    }
}

#48


这个题不难,只是有点繁杂而已.
其中的道理,和 编译原理 里的 "有限自动机" 有点类似.只需分析在每种状态下该怎么继续,就很好解决了.

#49


加了点注释,顺便整理了下.

using System;
using System.Collections;
using System.Collections.Generic;

namespace PrintNum
{
    enum 方向 { 向右=1,向下,向左,向上,向左下,向右上,向右下,向左上,无 };
    struct Point
    {
        public int x;
        public int y;
        public Point(int _x, int _y)
        { 
            x=_x;
            y=_y;
        }
    }
    class  ChangeInfo
    {
        public Point        pt起始点= new Point (0,0);//起始点坐标
         public int          i起始点值=1;//起始点值
         public int          i变化值=1;//变化值 1或-1
        public 方向           起始方向 = 方向.无;
        public Dictionary<方向, List<方向>> dic固定转换 = new Dictionary<方向, List<方向>>();//状态转化集合,有优先级
         public List<Point>  ls离散起始点 = new List<Point> (); //不连续的起始点集合
    }
    class csPrintNum
    {
        bool b找到下一点坐标 = false;
        方向 last方向 = 方向.无;
        int[,] matrix = null;
        int x; //横坐标 向右递增
         int y; //纵坐标 向下递增
         int v; //总标点值
         int n; //方块边长(规模)

        public csPrintNum(int _n, ChangeInfo _info)
        {
            n = _n;
            int iCount = n * n;
            matrix = new int[n, n];
            x = _info.pt起始点.x;
            y = _info.pt起始点.y;
            v = _info.i起始点值;
            matrix[x, y] = v;
            v += _info.i变化值;

            for (int i = 1; i < iCount; i++)//灌入数据到matrix
            {  
                if(i==1)
                     Move (_info .起始方向 );
                //从固定转换找下个坐标,dic固定转换里的链表有优先顺序
                  if (!b找到下一点坐标 && _info .dic固定转换.ContainsKey(last方向))
                {
                    List<方向> ls= _info .dic固定转换 [last方向 ] as List <方向>;
                    for (int k = 0;!b找到下一点坐标 &&  k < ls.Count; k++)
                    {
                        Move(ls[k]);
                    }
                }  
                //从离散点找下个坐标
                  if (!b找到下一点坐标 && _info.ls离散起始点.Count > 0)
                {
                    x = _info.ls离散起始点[0].x;
                    y = _info.ls离散起始点[0].y;
                    last方向 = 方向.无;
                    _info.ls离散起始点.RemoveAt(0);
                }
                //赋值
                  matrix[x, y] = v;

                //为下次循环做准备
                  v += _info.i变化值;
                b找到下一点坐标 = false;
            }
        }
        bool Move(方向 _方向)
        {
            if (_方向 == 方向.向右 && x < n - 1 && matrix[x + 1, y] == 0)
            {
                x += 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            else if (_方向 == 方向.向左 && x > 0 && matrix[x - 1, y] == 0)
            {
                x -= 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            else if (_方向 == 方向.向上 && y > 0 && matrix[x, y - 1] == 0)
            {
                y -= 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            else if (_方向 == 方向.向下 && y < n - 1 && matrix[x, y + 1] == 0)
            {
                y += 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            else if (_方向 == 方向.向左下 && x > 0 && y < n -1  && matrix[ x -1, y + 1] == 0)
            {
                x -= 1;
                y += 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            else if (_方向 == 方向.向右上 && x < n-1 && y > 0  && matrix[ x + 1, y - 1] == 0)
            {
                x += 1;
                y -= 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            else if (_方向 == 方向.向右下 && x < n - 1 && y < n-1 && matrix[x + 1, y + 1] == 0)
            {
                x += 1;
                y += 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            return false;
        }
        public void PrintMatrix()
        {
            if (matrix != null && matrix.Rank == 2)
            {
                for (int i = 0; i <= matrix.GetUpperBound(0); i++)
                {
                    for (int j = 0; j <= matrix.GetUpperBound(1); j++)
                    {
                        Console.Write(matrix[j, i].ToString("d2") + " ");
                    }
                    Console.WriteLine();
                }
                Console.WriteLine("-----------");
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int n=8;

            ChangeInfo info_1 = new ChangeInfo();
            info_1.起始方向 = 方向.向右;
            info_1.dic固定转换.Add(方向.向右, new List<方向>{ 方向.向右, 方向.向下 });
            info_1.dic固定转换.Add(方向.向下, new List<方向>{ 方向.向下, 方向.向左 });
            info_1.dic固定转换.Add(方向.向左, new List<方向>{ 方向.向左, 方向.向上 });
            info_1.dic固定转换.Add(方向.向上, new List<方向>{ 方向.向上, 方向.向右 });
            csPrintNum cs1 = new csPrintNum(n, info_1);
            cs1.PrintMatrix();

            ChangeInfo info_2 = new ChangeInfo();
            info_2.起始方向 = 方向.向右;
            info_2.dic固定转换.Add(方向.向右, new List<方向> { 方向.向左下, 方向.向右上 });
            info_2.dic固定转换.Add(方向.向下, new List<方向> { 方向.向右上, 方向.向左下 });
            info_2.dic固定转换.Add(方向.向左下, new List<方向>{ 方向.向左下, 方向.向下, 方向.向右 });
            info_2.dic固定转换.Add(方向.向右上, new List<方向>{ 方向.向右上, 方向.向右, 方向.向下 });
            csPrintNum cs2 = new csPrintNum(8, info_2);
            cs2.PrintMatrix();

            ChangeInfo info_3 = new ChangeInfo();
            info_3.dic固定转换.Add(方向.无, new List<方向>{ 方向.向左下 });
            info_3.dic固定转换.Add(方向.向左下, new List<方向>{ 方向.向左下 });
            for (int i = 1; i < n; i++)
                info_3.ls离散起始点.Add(new Point(i, 0));
            for (int i = 1; i < n; i++)
                info_3.ls离散起始点.Add(new Point(n - 1, i));
            csPrintNum cs3 = new csPrintNum(n, info_3);
            cs3.PrintMatrix();

            ChangeInfo info_4 = new ChangeInfo();
            info_4.dic固定转换.Add(方向.无, new List<方向>{ 方向.向右下 });
            info_4.dic固定转换.Add(方向.向右下, new List<方向>{ 方向.向右下 });
            for (int i = 1; i < n; i++)
            {
                info_4.ls离散起始点.Add(new Point(i, 0));
                info_4.ls离散起始点.Add(new Point(0, i));
            }
            csPrintNum cs4 = new csPrintNum(n, info_4);
            cs4.PrintMatrix();

            ChangeInfo info_5 = new ChangeInfo();
            info_5.i起始点值 = 64;
            info_5.i变化值 = -1;
            info_5.起始方向 = 方向.向右;
            info_5.dic固定转换.Add(方向.向右, new List<方向>{ 方向.向右, 方向.向下 });
            info_5.dic固定转换.Add(方向.向下, new List<方向>{ 方向.向右, 方向.向下, 方向.向左 });
            info_5.dic固定转换.Add(方向.向左, new List<方向>{ 方向.向上, 方向.向左, 方向.向下 });
            info_5.dic固定转换.Add(方向.向上, new List<方向>{ 方向.向上, 方向.向左 });
            csPrintNum cs5 = new csPrintNum(n, info_5);
            cs5.PrintMatrix();

            ChangeInfo info_6 = new ChangeInfo();
            info_6.起始方向 = 方向.向右;
            info_6.dic固定转换.Add(方向.向右, new List<方向>{ 方向.向右, 方向.向下 });
            info_6.dic固定转换.Add(方向.向下, new List<方向>{ 方向.向右, 方向.向左 });
            info_6.dic固定转换.Add(方向.向左, new List<方向>{ 方向.向左, 方向.向下 });
            csPrintNum cs6 = new csPrintNum(n, info_6);
            cs6.PrintMatrix();
        }
    }
}

#50


引用 47 楼 fckadxz 的回复:
花了几个小时,6个图都比较圆满地解决了.
可读行和扩展性 马马虎虎,呵呵.不废话,上code.
[img=http://p.blog.csdn.net/images/p_blog_csdn_net/renrousousuo/42.gif]图[/img] 写得挺精彩,一贴搞定是我期待的!

不过有点问题,当N=9的时候,输出:
详情
01 02 03 04 05 06 07 08 09
32 33 34 35 36 37 38 39 10
31 56 57 58 59 60 61 40 11
30 55 72 73 74 75 62 41 12
29 54 71 80 81 76 63 42 13
28 53 70 79 78 77 64 43 14
27 52 69 68 67 66 65 44 15
26 51 50 49 48 47 46 45 16
25 24 23 22 21 20 19 18 17
-----------
01 02 06 07 15 16 28 29
03 05 08 14 17 27 30 43
04 09 13 18 26 31 42 44
10 12 19 25 32 41 45 54
11 20 24 33 40 46 53 55
21 23 34 39 47 52 56 61
22 35 38 48 51 57 60 62
36 37 49 50 58 59 63 64
-----------
01 02 04 07 11 16 22 29 37
03 05 08 12 17 23 30 38 46
06 09 13 18 24 31 39 47 54
10 14 19 25 32 40 48 55 61
15 20 26 33 41 49 56 62 67
21 27 34 42 50 57 63 68 72
28 35 43 51 58 64 69 73 76
36 44 52 59 65 70 74 77 79
45 53 60 66 71 75 78 80 81
-----------
01 10 26 40 52 62 70 76 80
18 02 11 27 41 53 63 71 77
33 19 03 12 28 42 54 64 72
46 34 20 04 13 29 43 55 65
57 47 35 21 05 14 30 44 56
66 58 48 36 22 06 15 31 45
73 67 59 49 37 23 07 16 32
78 74 68 60 50 38 24 08 17
81 79 75 69 61 51 39 25 09
-----------
64 63 62 61 60 59 58 57 56
33 34 35 36 37 38 39 40 55
32 31 30 29 28 27 26 41 54
09 10 11 12 13 14 25 42 53
08 07 06 05 04 15 24 43 52
-07 -06 -05 -04 03 16 23 44 51
-08 -09 -10 -03 02 17 22 45 50
-15 -14 -11 -02 01 18 21 46 49
-16 -13 -12 -01 00 19 20 47 48
-----------
01 02 03 04 05 06 07 08 09
18 17 16 15 14 13 12 11 10
19 20 21 22 23 24 25 26 27
36 35 34 33 32 31 30 29 28
37 38 39 40 41 42 43 44 45
54 53 52 51 50 49 48 47 46
55 56 57 58 59 60 61 62 63
72 71 70 69 68 67 66 65 64
73 74 75 76 77 78 79 80 81
-----------

起始点可以用边界来代表,谢谢参与!

#1


有难度   先顶下再说

#2


收藏

#3


看上去很难啊。 帮你顶啊!

#4


kankan

#5


还有哈..
顶...

#6


楼主的题太难了,估计这次一倒一大片了。

BTW,最近楼主分是不是多的溢出了 n_n

#7


mark先~

#8


又来了 上次作业还没交呢

#9


难度有点大!!

#10


嗯,可以酌情散掉了

#11


引用 6 楼 nattystyle 的回复:
楼主的题太难了,估计这次一倒一大片了。

BTW,最近楼主分是不是多的溢出了 n_n
那确实。。。还有30000多分不知道怎么花。。。[img=http://p.blog.csdn.net/images/p_blog_csdn_net/zswangii/%E6%8C%96%E9%BC%BB%E5%AD%94.gif]图[/img]

#12


题目好多啊
就做第一题吧


        private const int WIDTH = 8;
        private int Min(params int[] vars) // 取最小值
        { 
            //略
        } 

        // 第几圈
        private int GetCircle(Point p)
        {
            return Min(p.X, p.Y, WIDTH - p.X, WIDTH - p.Y);
        }

        // 方向
        private int GetDirection(Point p)
        {
            switch (GetCircle(p))
            {
                case p.Y: return 1;
                case p.X: return 2;
                case WIDTH - p.Y: return 3;
                case WIDTH - p.X: return 4;
            }
        }

        // 顺序
        private int GetSequence(Point p)
        {
            switch (GetDirection(p))
            {
                case 1: return p.X - GetCircle(p) + 1;
                case 2: return p.Y - GetCircle(p) + 1;
                case 3: return WIDTH - GetCircle(p) + 1 - p.X;
                case 4: return WIDTH - GetCircle(p) + 1 - p.Y;
            }
        }

        private int GetValue(Point p)
        {
            int Value = 0;

            for(int i=1;i<GetCircle(p);i++)
            {
                Value += 4 * (WIDTH - 1 - i * 2);
            }

            Value += (GetDirection(p) - 1) * (WIDTH - 1 - (GetCircle(p) - 1) * 2) + GetSequence(p);

            return Value;
        }

#13


我也简单实现了一下第一题:
using System;

public class ShowData
{
    public static void Main( string[] args )
    {
        if(args.Length <= 0){
            Console.WriteLine("至少输入一个数字");
            return;
        }
        // 获取输入的行和列
        int x,y;
        try{
            x = int.Parse(args[0]);
            if(args.Length > 1)
                y = int.Parse(args[1]);
            else
                y = x;
        }catch{
            Console.WriteLine("请输入数字");
            return;
        }
        
        int[,] arr = new int[x,y];
        InsertArr(arr, 1, 0);
        OutPut(arr);
    }
    
    private static void InsertArr(int[,] arr, int level, int numBegin){
        int x = arr.GetLength(0);
        int y = arr.GetLength(1);

        // 判断是否最内圈
        if(numBegin >= (x * y))
            return;
        
        // 第一行
        for(int i=level-1;i<=y-level;i++){
            numBegin++;
            arr[level-1,i] = numBegin;
        }
        // 中间行的最后一列
        for(int i=level;i<x-level;i++){
            numBegin++;
            arr[i,y-level] = numBegin;
        }
        // 最后一行
        for(int i=y-level;i>=level-1;i--){
            numBegin++;
            arr[x-level,i] = numBegin;
        }
        // 中间行的第一列
        for(int i=x-level-1;i>=level;i--){
            numBegin++;
            arr[i,level-1] = numBegin;
        }
        
        // 递归
        InsertArr(arr, level+1, numBegin);
    }
    
    private static void OutPut(int[,] arr){
        for(int i=0;i<arr.GetLength(0);i++){
            for(int j=0;j<arr.GetLength(1);j++){
                Console.Write(arr[i,j].ToString("00") + ",");
            }
            Console.WriteLine();
        }
    }
}

#14


挖塞!
MARK

#15


比较烦琐

#16


注:
说明一下,我的程序扩展了一下题目,可以输入一个参数,也可以输入2个参数
如果只输入一个参数,那么输出就是一个正方形

如果输入2个参数,那么输出就是一个长方形

#17


看就好了 写这个东西还是C++好用

#18



using System;
namespace PrintBox
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("请输入2--9的整数:");
            string input = Console.ReadLine();
            int row = 0;
            while (!int.TryParse(input, out row) || (row > 9 || row < 2))
            {
                Console.Write("请输入2--9的整数:");
                input = Console.ReadLine();
            }
            PrintBox(row);
            Console.ReadKey();
        }
        public static void PrintBox(int row)
        {
            int[,] array = new int[row, row];
            int index = 1;
            int j = 0, k = 0;

            #region first
            int right = row - 1, left = 0, bottom = row - 1, top = 1;
            for (int i = 0; i < row / 2 + row % 2; i++)
            {
                while (k <= right)
                    array[j, k++] = index++;
                k--;
                j++;
                while (j <= bottom)
                    array[j++, k] = index++;
                j--;
                k--;
                while (k >= left)
                    array[j, k--] = index++;
                k++;
                j--;
                while (j >= top)
                    array[j--, k] = index++;
                j++;
                k++;
                right--;
                left++;
                bottom--;
                top++;
            }
            for (int a = 0; a < row; a++)
            {
                for (int b = 0; b < row; b++)
                    Console.Write(array[a, b].ToString().PadLeft(2, '0') + " ");
                Console.WriteLine();
            }
            Console.WriteLine("-----");
            #endregion

            #region second
            index = 1;
            j = k = 0;
            for (int i = 0; i < 2 * row - 1; i++)
            {
                while (j + k == i)
                {
                    array[j, k] = index++;
                    if (j == 0 && k == 0)
                    {
                        k++;
                        continue;
                    }
                    else
                    {
                        if (j == 0)
                        {
                            while (j != i)
                            {
                                j++;
                                k--;
                                array[j, k] = index++;
                            }
                            if (j != row - 1)
                                j++;
                            else
                                k++;
                            break;
                        }
                        if (j == row - 1)
                        {
                            while (k != row - 1)
                            {
                                j--;
                                k++;
                                array[j, k] = index++;
                            }
                            j++;
                            break;
                        }
                        //k++;
                        if (k == 0)
                        {
                            while (k != i)
                            {
                                j--;
                                k++;
                                array[j, k] = index++;
                            }
                            if (k != row - 1)
                                k++;
                            else
                                j++;
                            break;
                        }
                        if (k == row - 1)
                        {
                            while (j != row - 1)
                            {
                                j++;
                                k--;
                                array[j, k] = index++;
                            }
                            k++;
                            break;
                        }
                        //j++;
                    }
                }
            }

            for (int a = 0; a < row; a++)
            {
                for (int b = 0; b < row; b++)
                    Console.Write(array[a, b].ToString().PadLeft(2, '0') + " ");
                Console.WriteLine();
            }
            Console.WriteLine("-----");
            #endregion

            #region third
            index = 1;
            j = k = 0;
            for (int i = 0; i < 2 * row - 1; i++)
            {
                while (j + k == i)
                {
                    array[j, k] = index++;
                    if (j == 0 && k == 0)
                    {
                        k++;
                        continue;
                    }
                    else
                    {
                        if (j == 0)
                        {
                            while (j != i)
                            {
                                j++;
                                k--;
                                array[j, k] = index++;
                            }
                            if (j == row - 1 && k == 0)
                            {
                                j = k + 1;
                                k = row - 1;
                                break;
                            }
                            if (j != row - 1)
                            {
                                k = j + 1;
                                j = 0;
                            }
                        }
                        if (k == row - 1 && j != 0)
                        {
                            while (j != row - 1)
                            {
                                j++;
                                k--;
                                array[j, k] = index++;
                            }
                            if (k == row - 1 && j == row - 1)
                                break;
                            if (k != row - 1)
                            {
                                j = k + 1;
                                k = row - 1;
                            }
                        }
                    }
                }
            }

            for (int a = 0; a < row; a++)
            {
                for (int b = 0; b < row; b++)
                    Console.Write(array[a, b].ToString().PadLeft(2, '0') + " ");
                Console.WriteLine();
            }
            Console.WriteLine("-----");

            #endregion

#19


接上面

            #region fourth
            index = 1;
            j = k = 0;
            for (int i = 0; i < row; i++)
            {
                j = 0;
                k = i;
                for (j = 0; k < row; )
                {
                    array[j, k] = index++;
                    j++;
                    k++;
                }
                if (i != 0)
                {
                    j = i;
                    k = 0;
                    for (k = 0; j < row; )
                    {
                        array[j, k] = index++;
                        j++;
                        k++;
                    }
                }
            }

            for (int a = 0; a < row; a++)
            {
                for (int b = 0; b < row; b++)
                    Console.Write(array[a, b].ToString().PadLeft(2, '0') + " ");
                Console.WriteLine();
            }
            Console.WriteLine("-----");

            #endregion

            #region fifth
            index = row * row;
            j = k = 0;
            right = row - 1;
            left = 0;
            bottom = row - 1;
            top = 1;
            for (int i = 0; i < row / 2 + row % 2; i++)
            {
                while (k <= right)
                    array[j, k++] = index--;
                j++;
                k--;
                while (j <= bottom)
                    array[j++, k] = index--;
                k--;
                j--;
                while (j >= top)
                    array[j--, k] = index--;
                k--;
                j++;
                while (k >= left)
                    array[j, k--] = index--;
                j++;
                k++;
                right -= 2;
                top += 2;
            }
            for (int a = 0; a < row; a++)
            {
                for (int b = 0; b < row; b++)
                    Console.Write(array[a, b].ToString().PadLeft(2, '0') + " ");
                Console.WriteLine();
            }
            Console.WriteLine("-----");

            #endregion

            #region sixth
            index = 1;
            j = k = 0;
            for (int i = 0; i < row; i++)
            {
                if (k == 0)
                {
                    for (; k < row; )
                    {
                        array[j, k] = index++;
                        k++;
                    }
                    j++;
                    if (k == row)
                        k--;
                    continue;
                }
                if (k == row - 1)
                {
                    for (; k >= 0; )
                    {
                        array[j, k] = index++;
                        k--;
                    }
                    j++;
                    if (k < 0)
                        k++;
                    continue;
                }
            }

            for (int a = 0; a < row; a++)
            {
                for (int b = 0; b < row; b++)
                    Console.Write(array[a, b].ToString().PadLeft(2, '0') + " ");
                Console.WriteLine();
            }
            Console.WriteLine("-----");
            #endregion
        }

    }
}

#20


6个图形在一个程序中输出吗?

#21


输出“00”可以这样:
Console.Write("{0}{1:D2}", i > 0 ? " " : "", Numbers[i, j]);

#22


引用 20 楼 wdgphc 的回复:
6个图形在一个程序中输出吗?
是的。

#23


这次要求可读性了啊这次. 中午吃饭的时候想想看看.

#24


不错

#25


收藏一下 有空做

#26


纯粹为了接分而来!

#27


帅气 就喜欢这样的题
今天活忙  有时间研究下
感觉不难实现
俺是学数学的!
为数学骄傲

#28


很牛????
要学习学习

#29


先收藏了,很好的学习和锻炼的题目!!

#30


学习,收藏

#31


时间来不及了,先做了2个.

        private void p1(int n)
        {
            int[,] array = new int[n, n];
            array[0, 0] = 1;
            int gridx = 0, gridy = 0, forward = 6;
            for (int i = 2; i <= n * n; i++)
            {
                switch (forward)
                {
                    case 2:
                        if ((gridy - 1 >= 0) && (array[gridx, gridy - 1] == 0))
                            array[gridx, --gridy] = i;
                        else
                        {
                            array[++gridx, gridy] = i;
                            forward = 6;
                        }
                        break;
                    case 4:
                        if ((gridx - 1 >= 0) && (array[gridx - 1, gridy] == 0))
                            array[--gridx, gridy] = i;
                        else
                        {
                            array[gridx, --gridy] = i;
                            forward = 2;
                        }
                        break;
                    case 6:
                        if ((gridx + 1 < n) && (array[gridx + 1, gridy] == 0))
                            array[++gridx, gridy] = i;
                        else
                        {
                            array[gridx, ++gridy] = i;
                            forward = 8;
                        }
                        break;
                    case 8:
                        if ((gridy + 1 < n) && (array[gridx, gridy + 1] == 0))
                            array[gridx, ++gridy] = i;
                        else
                        {
                            array[--gridx, gridy] = i;
                            forward = 4;
                        }
                        break;
                }
            }
            printArray(array, n);
        }

        private void p2(int n)
        {
            int[,] array = new int[n, n];
            array[0, 0] = 1; array[1, 0] = 2;
            int gridx = 1, gridy = 0, forward = 7;
            for (int i = 3; i <= n * n; i++)
            {
                switch (forward)
                {
                    case 3:
                        if ((gridx + 1 < n) && (gridy - 1 >= 0) && (array[gridx + 1, gridy - 1] == 0))
                            array[++gridx, --gridy] = i;
                        else
                        {
                            if (gridx + 1 < n)
                                array[++gridx, gridy] = i;
                            else
                                array[gridx, ++gridy] = i;
                            forward = 7;
                        }
                        break;
                    case 7:
                        if ((gridx - 1 >= 0) && (gridy + 1 < n) && (array[gridx - 1, gridy + 1] == 0))
                            array[--gridx, ++gridy] = i;
                        else
                        {
                            if (gridy + 1 < n)
                                array[gridx, ++gridy] = i;
                            else
                                array[++gridx, gridy] = i;
                            forward = 3;
                        }
                        break;
                }
            }
            printArray(array, n);
        }

        private void printArray(int[,] array, int num)
        {
            for (int i = 0; i < num; i++)
            {
                for (int j = 0; j < num; j++)
                    Console.Write(array[j, i].ToString("00") + " ");
                Console.WriteLine();
            }
        }


调用
p1(8);
p2(8);

#32


using System;
namespace PrintArray
{
    class PrintArray
    {
        static private void p1(int n)
        {
            int[,] array = new int[n, n];
            array[0, 0] = 1;
            int gridx = 0, gridy = 0, forward = 6;
            for (int i = 2; i <= n * n; i++)
            {
                switch (forward)
                {
                    case 2:
                        if ((gridy - 1 >= 0) && (array[gridx, gridy - 1] == 0))
                            array[gridx, --gridy] = i;
                        else
                        {
                            array[++gridx, gridy] = i;
                            forward = 6;
                        }
                        break;
                    case 4:
                        if ((gridx - 1 >= 0) && (array[gridx - 1, gridy] == 0))
                            array[--gridx, gridy] = i;
                        else
                        {
                            array[gridx, --gridy] = i;
                            forward = 2;
                        }
                        break;
                    case 6:
                        if ((gridx + 1 < n) && (array[gridx + 1, gridy] == 0))
                            array[++gridx, gridy] = i;
                        else
                        {
                            array[gridx, ++gridy] = i;
                            forward = 8;
                        }
                        break;
                    case 8:
                        if ((gridy + 1 < n) && (array[gridx, gridy + 1] == 0))
                            array[gridx, ++gridy] = i;
                        else
                        {
                            array[--gridx, gridy] = i;
                            forward = 4;
                        }
                        break;
                }
            }
            printArray(array, n);
        }
        static private void p2(int n)
        {
            int[,] array = new int[n, n];
            array[0, 0] = 1; array[1, 0] = 2;
            int gridx = 1, gridy = 0, forward = 7;
            for (int i = 3; i <= n * n; i++)
            {
                switch (forward)
                {
                    case 3:
                        if ((gridx + 1 < n) && (gridy - 1 >= 0))
                            array[++gridx, --gridy] = i;
                        else
                        {
                            if (gridx + 1 < n)
                                array[++gridx, gridy] = i;
                            else
                                array[gridx, ++gridy] = i;
                            forward = 7;
                        }
                        break;
                    case 7:
                        if ((gridx - 1 >= 0) && (gridy + 1 < n))
                            array[--gridx, ++gridy] = i;
                        else
                        {
                            if (gridy + 1 < n)
                                array[gridx, ++gridy] = i;
                            else
                                array[++gridx, gridy] = i;
                            forward = 3;
                        }
                        break;
                }
            }
            printArray(array, n);
        }
        static private void p3(int n)
        {
            int[,] array = new int[n, n];
            array[0, 0] = 1;
            int gridx = 0, gridy = 0;
            for (int i = 2; i <= n * n; i++)
            {
                if ((gridx - 1 >= 0) && (gridy + 1 < n))
                    array[--gridx, ++gridy] = i;
                else
                {
                    if (gridy + 1 < n)
                        array[gridx = gridy + 1, gridy = 0] = i;
                    else
                    {
                        gridy = gridx + 1;
                        gridx = n - 1;
                        array[gridx, gridy] = i;
                    }
                }
            }
            printArray(array, n);
        }
        static private void p4(int n)
        {
            int[,] array = new int[n, n];
            array[0, 0] = 1;
            int gridx = 0, gridy = 0;
            for (int i = 2; i <= n * n; i++)
            {
                if ((gridx + 1 < n) && (gridy + 1 < n))
                    array[++gridx, ++gridy] = i;
                else
                {
                    if (gridy < gridx)
                    {
                        array[gridx = 0, gridy = n - gridy - 1] = i;
                    }
                    else
                    {
                        array[gridx = n - gridx, gridy = 0] = i;
                    }
                }
            }
            printArray(array, n);
        }
        static private void p5(int n)
        {
            int[,] array = new int[n, n];
            array[0, n - 1] = 1;
            int gridx = 0, gridy = n - 1, forward = 8;
            for (int i = 2; i <= n * n; i++)
            {
                switch (forward)
                {
                    case 2:
                        array[gridx, --gridy] = i;
                        if (gridx + gridy + 1 == n)
                        {
                            forward = 4;
                        }
                        break;
                    case 4:
                        if (gridx - 1 >= 0)
                            array[--gridx, gridy] = i;
                        else
                        {
                            array[gridx, --gridy] = i;
                            forward = 6;
                        }
                        break;
                    case 6:
                        array[++gridx, gridy] = i;
                        if (gridx + gridy + 1 == n)
                        {
                            forward = 8;
                        }
                        break;
                    case 8:
                        if (gridy + 1 < n)
                            array[gridx, ++gridy] = i;
                        else
                        {
                            array[++gridx, gridy] = i;
                            forward = 2;
                        }
                        break;
                }
            }
            printArray(array, n);
        }
        static private void p6(int n)
        {
            int[,] array = new int[n, n];
            array[0, 0] = 1;
            int gridx = 0, gridy = 0, forward = 6;
            for (int i = 2; i <= n * n; i++)
            {
                switch (forward)
                {
                    case 4:
                        if (gridx - 1 >= 0)
                            array[--gridx, gridy] = i;
                        else
                        {
                            array[gridx, ++gridy] = i;
                            forward = 6;
                        }
                        break;
                    case 6:
                        if (gridx + 1 < n)
                            array[++gridx, gridy] = i;
                        else
                        {
                            array[gridx, ++gridy] = i;
                            forward = 4;
                        }
                        break;
                }
            }
            printArray(array, n);
        }
        static private void printArray(int[,] array, int num)
        {
            for (int i = 0; i < num; i++)
            {
                for (int j = 0; j < num; j++)
                    Console.Write(array[j, i].ToString("00") + " ");
                Console.WriteLine();
            }
            Console.WriteLine("-----------------------");
        }
    }
}

#33


只能输入8000个字符啊,只好把Main函数扔到这楼了.

static void Main()
        {
            char c = '0';
            while (c != 'n' && c != 'N')
            {
                Console.Write("输入2-9:");
                c = Console.ReadKey().KeyChar;
                Console.WriteLine();
                if (c < '2' || c > '9') continue;
                int n = c - '0';
                p1(n);
                p2(n);
                p3(n);
                p4(n);
                p5(n);
                p6(n);
            }
        }


Mian函数放在32楼的Class中 为完整代码 --- wdghc . 时间仓促,不足请指教.

#34


总觉得应该有更好办法的  而不是一个个输出来

#35


这个题真的很考人的逻辑思维, 有时间来  做一下!!!!!

#36


引用 8 楼 LQknife 的回复:
又来了 上次作业还没交呢 

#37


感谢charley450、ojlovecd、wdgphc参与

目前还没有出现更精彩的代码,加100继续期待。

#38


楼主分多的溢水了汗死了。

#39


又散分了?jfjf
每天接分,提高知名度

#40


引用 37 楼 zswang 的回复:
感谢charley450、ojlovecd、wdgphc参与 

目前还没有出现更精彩的代码,加100继续期待。


呵呵,我一般都是能实现为主,请给个努力的方向-> 什么方式为精彩完美?

#41


我总共才200多分,所以不懂也要帮顶

#42


只写一个可以吗?
写第二个吧


using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int len = 0;
            if (int.TryParse(Console.ReadLine().Substring(0, 1),out len))
                output(matrix(len));
            Console.ReadLine();
        }

        static int[,] matrix(int len)
        {
            int num = 1;
            int[,] matrix = new int[len, len];

            int n = 2 * (len - 1);

            int x = 0;
            for (int i = 0; i <= 2 * len - 1; i++)
            {
                if (i < len)
                    x = 0;
                else
                    x = i - len + 1;
                while (x <= i && x < len && i - x < len)
                {
                    if (i % 2 == 1)
                    {
                        matrix[x, i - x] = num;
                    }
                    else
                    {
                        matrix[i - x, x] = num;
                    }
                    x++;
                    num++;
                }
            }

            return matrix;
        }

        static void output(int[,] matrix)
        {
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    Console.Write(matrix[i, j].ToString().PadLeft(2, '0') + "   ");
                }
                Console.WriteLine();
            }
        }
    }
}

#43


引用 40 楼 wdgphc 的回复:
呵呵,我一般都是能实现为主,请给个努力的方向-> 什么方式为精彩完美?
呵呵,如果有新的图像加入现在的代码又的重新构建一遍,总得来说就是代码重复利用率不高。
可以考虑采用面向对象的方法。[img=http://p.blog.csdn.net/images/p_blog_csdn_net/renrousousuo/49.gif]图[/img]

#44


除了最后一题目和第1题,
其他都超级难啊!

#45


关注!

#46


继续关注!

#47


花了几个小时,6个图都比较圆满地解决了.
可读行和扩展性 马马虎虎,呵呵.不废话,上code.

using System;
using System.Collections;
using System.Collections.Generic;

namespace PrintNum2
{
    enum 方向 { 向右=1,向下,向左,向上,向左下,向右上,向右下,向左上,无 };
    struct Point
    {
        public int x;
        public int y;
        public Point(int _x, int _y)
        { 
            x=_x;
            y=_y;
        }
    }
    class  ChangeInfo
    {
        public Point        pt原点= new Point (0,0);//起始点坐标
        public int          i原点值=1;//起始点值
        public int          i变化值=1;//变化值 1或-1
        public 方向         起始方向 = 方向.无;
        public Dictionary<方向, List<方向>> dic固定转换 = new Dictionary<方向, List<方向>>();
        public List<Point>  ls离散起始点 = new List<Point> ();
    }
    class csPrintNum
    {
        bool b找到下一点坐标 = false;
        int[,] matrix = null;
        int x;
        int y;
        int v;
        int n;
        方向 last方向 = 方向.无;
        public csPrintNum(int _n, ChangeInfo _info)
        {
            n = _n;
            int iCount = n * n;
            matrix = new int[n, n];
            x = _info.pt原点.x;
            y = _info.pt原点.y;
            v = _info.i原点值;
            matrix[x, y] = v;
            v += _info.i变化值;

            for (int i = 1; i < iCount; i++)//灌入数据_n*_n次到matrix
            {  
                if(i==1)
                     Move (_info .起始方向 );
                if (!b找到下一点坐标 && _info .dic固定转换.ContainsKey(last方向))
                {
                    List<方向> ls= _info .dic固定转换 [last方向 ] as List <方向>;
                    for (int k = 0;!b找到下一点坐标 &&  k < ls.Count; k++)
                    {
                        Move(ls[k]);
                    }
                }  
                //从离散点找下个坐标
                if (!b找到下一点坐标 && _info.ls离散起始点.Count > 0)
                {
                    x = _info.ls离散起始点[0].x;
                    y = _info.ls离散起始点[0].y;
                    last方向 = 方向.无;
                    _info.ls离散起始点.RemoveAt(0);
                }
                matrix[x, y] = v;
                v += _info.i变化值;
                b找到下一点坐标 = false;
            }
        }
        bool Move(方向 _方向)
        {
            if (_方向 == 方向.向右 && x < n - 1 && matrix[x + 1, y] == 0)
            {
                x += 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            else if (_方向 == 方向.向左 && x > 0 && matrix[x - 1, y] == 0)
            {
                x -= 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            else if (_方向 == 方向.向上 && y > 0 && matrix[x, y - 1] == 0)
            {
                y -= 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            else if (_方向 == 方向.向下 && y < n - 1 && matrix[x, y + 1] == 0)
            {
                y += 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            else if (_方向 == 方向.向左下 && x > 0 && y < n -1  && matrix[ x -1, y + 1] == 0)
            {
                x -= 1;
                y += 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            else if (_方向 == 方向.向右上 && x < n-1 && y > 0  && matrix[ x + 1, y - 1] == 0)
            {
                x += 1;
                y -= 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            else if (_方向 == 方向.向右下 && x < n - 1 && y < n-1 && matrix[x + 1, y + 1] == 0)
            {
                x += 1;
                y += 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            return false;
        }
        public void PrintMatrix()
        {
            if (matrix != null && matrix.Rank == 2)
            {
                for (int i = 0; i <= matrix.GetUpperBound(0); i++)
                {
                    for (int j = 0; j <= matrix.GetUpperBound(1); j++)
                    {
                        Console.Write(matrix[j, i].ToString("d2") + " ");
                    }
                    Console.WriteLine();
                }
                Console.WriteLine("-----------");
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int n=8;
            ChangeInfo info_1 = new ChangeInfo();
            info_1.起始方向 = 方向.向右;
            info_1.dic固定转换.Add(方向.向右, new List<方向>(new 方向[] { 方向.向右, 方向.向下 }));
            info_1.dic固定转换.Add(方向.向下, new List<方向>(new 方向[] { 方向.向下, 方向.向左 }));
            info_1.dic固定转换.Add(方向.向左, new List<方向>(new 方向[] { 方向.向左, 方向.向上 }));
            info_1.dic固定转换.Add(方向.向上, new List<方向>(new 方向[] { 方向.向上, 方向.向右 }));

            csPrintNum cs1 = new csPrintNum(n, info_1);
            cs1.PrintMatrix();

            ChangeInfo info_2 = new ChangeInfo();
            info_2.起始方向 = 方向.向右;
            info_2.dic固定转换.Add(方向.向右, new List<方向>(new 方向[] { 方向.向左下, 方向.向右上 }));
            info_2.dic固定转换.Add(方向.向下, new List<方向>(new 方向[] { 方向.向右上, 方向.向左下 }));
            info_2.dic固定转换.Add(方向.向左下, new List<方向>(new 方向[] { 方向.向左下, 方向.向下, 方向.向右 }));
            info_2.dic固定转换.Add(方向.向右上, new List<方向>(new 方向[] { 方向.向右上, 方向.向右, 方向.向下 }));
            csPrintNum cs2 = new csPrintNum(8, info_2);
            cs2.PrintMatrix();

            ChangeInfo info_3 = new ChangeInfo();
            info_3.dic固定转换.Add(方向.无, new List<方向>(new 方向[] { 方向.向左下 }));
            info_3.dic固定转换.Add(方向.向左下, new List<方向>(new 方向[] { 方向.向左下 }));
            for (int i = 1; i < n; i++)
                info_3.ls离散起始点.Add(new Point(i, 0));
            for (int i = 1; i < n; i++)
                info_3.ls离散起始点.Add(new Point(n - 1, i));
            csPrintNum cs3 = new csPrintNum(n, info_3);
            cs3.PrintMatrix();

            ChangeInfo info_4 = new ChangeInfo();
            info_4.dic固定转换.Add(方向.无, new List<方向>(new 方向[] { 方向.向右下 }));
            info_4.dic固定转换.Add(方向.向右下, new List<方向>(new 方向[] { 方向.向右下 }));
            for (int i = 1; i < n; i++)
            {
                info_4.ls离散起始点.Add(new Point(i, 0));
                info_4.ls离散起始点.Add(new Point(0, i));
            }
            csPrintNum cs4 = new csPrintNum(n, info_4);
            cs4.PrintMatrix();

            ChangeInfo info_5 = new ChangeInfo();
            info_5.i原点值 = 64;
            info_5.i变化值 = -1;
            info_5.起始方向 = 方向.向右;
            info_5.dic固定转换.Add(方向.向右, new List<方向>(new 方向[] { 方向.向右, 方向.向下 }));
            info_5.dic固定转换.Add(方向.向下, new List<方向>(new 方向[] { 方向.向右, 方向.向下, 方向.向左 }));
            info_5.dic固定转换.Add(方向.向左, new List<方向>(new 方向[] { 方向.向上, 方向.向左, 方向.向下 }));
            info_5.dic固定转换.Add(方向.向上, new List<方向>(new 方向[] { 方向.向上, 方向.向左 }));
            csPrintNum cs5 = new csPrintNum(n, info_5);
            cs5.PrintMatrix();

            ChangeInfo info_6 = new ChangeInfo();
            info_6.起始方向 = 方向.向右;
            info_6.dic固定转换.Add(方向.向右, new List<方向>(new 方向[] { 方向.向右, 方向.向下 }));
            info_6.dic固定转换.Add(方向.向下, new List<方向>(new 方向[] { 方向.向右, 方向.向左 }));
            info_6.dic固定转换.Add(方向.向左, new List<方向>(new 方向[] { 方向.向左, 方向.向下 }));
            csPrintNum cs6 = new csPrintNum(n, info_6);
            cs6.PrintMatrix();
        }
    }
}

#48


这个题不难,只是有点繁杂而已.
其中的道理,和 编译原理 里的 "有限自动机" 有点类似.只需分析在每种状态下该怎么继续,就很好解决了.

#49


加了点注释,顺便整理了下.

using System;
using System.Collections;
using System.Collections.Generic;

namespace PrintNum
{
    enum 方向 { 向右=1,向下,向左,向上,向左下,向右上,向右下,向左上,无 };
    struct Point
    {
        public int x;
        public int y;
        public Point(int _x, int _y)
        { 
            x=_x;
            y=_y;
        }
    }
    class  ChangeInfo
    {
        public Point        pt起始点= new Point (0,0);//起始点坐标
         public int          i起始点值=1;//起始点值
         public int          i变化值=1;//变化值 1或-1
        public 方向           起始方向 = 方向.无;
        public Dictionary<方向, List<方向>> dic固定转换 = new Dictionary<方向, List<方向>>();//状态转化集合,有优先级
         public List<Point>  ls离散起始点 = new List<Point> (); //不连续的起始点集合
    }
    class csPrintNum
    {
        bool b找到下一点坐标 = false;
        方向 last方向 = 方向.无;
        int[,] matrix = null;
        int x; //横坐标 向右递增
         int y; //纵坐标 向下递增
         int v; //总标点值
         int n; //方块边长(规模)

        public csPrintNum(int _n, ChangeInfo _info)
        {
            n = _n;
            int iCount = n * n;
            matrix = new int[n, n];
            x = _info.pt起始点.x;
            y = _info.pt起始点.y;
            v = _info.i起始点值;
            matrix[x, y] = v;
            v += _info.i变化值;

            for (int i = 1; i < iCount; i++)//灌入数据到matrix
            {  
                if(i==1)
                     Move (_info .起始方向 );
                //从固定转换找下个坐标,dic固定转换里的链表有优先顺序
                  if (!b找到下一点坐标 && _info .dic固定转换.ContainsKey(last方向))
                {
                    List<方向> ls= _info .dic固定转换 [last方向 ] as List <方向>;
                    for (int k = 0;!b找到下一点坐标 &&  k < ls.Count; k++)
                    {
                        Move(ls[k]);
                    }
                }  
                //从离散点找下个坐标
                  if (!b找到下一点坐标 && _info.ls离散起始点.Count > 0)
                {
                    x = _info.ls离散起始点[0].x;
                    y = _info.ls离散起始点[0].y;
                    last方向 = 方向.无;
                    _info.ls离散起始点.RemoveAt(0);
                }
                //赋值
                  matrix[x, y] = v;

                //为下次循环做准备
                  v += _info.i变化值;
                b找到下一点坐标 = false;
            }
        }
        bool Move(方向 _方向)
        {
            if (_方向 == 方向.向右 && x < n - 1 && matrix[x + 1, y] == 0)
            {
                x += 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            else if (_方向 == 方向.向左 && x > 0 && matrix[x - 1, y] == 0)
            {
                x -= 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            else if (_方向 == 方向.向上 && y > 0 && matrix[x, y - 1] == 0)
            {
                y -= 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            else if (_方向 == 方向.向下 && y < n - 1 && matrix[x, y + 1] == 0)
            {
                y += 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            else if (_方向 == 方向.向左下 && x > 0 && y < n -1  && matrix[ x -1, y + 1] == 0)
            {
                x -= 1;
                y += 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            else if (_方向 == 方向.向右上 && x < n-1 && y > 0  && matrix[ x + 1, y - 1] == 0)
            {
                x += 1;
                y -= 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            else if (_方向 == 方向.向右下 && x < n - 1 && y < n-1 && matrix[x + 1, y + 1] == 0)
            {
                x += 1;
                y += 1;
                last方向 = _方向;
                b找到下一点坐标 = true;
                return true;
            }
            return false;
        }
        public void PrintMatrix()
        {
            if (matrix != null && matrix.Rank == 2)
            {
                for (int i = 0; i <= matrix.GetUpperBound(0); i++)
                {
                    for (int j = 0; j <= matrix.GetUpperBound(1); j++)
                    {
                        Console.Write(matrix[j, i].ToString("d2") + " ");
                    }
                    Console.WriteLine();
                }
                Console.WriteLine("-----------");
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int n=8;

            ChangeInfo info_1 = new ChangeInfo();
            info_1.起始方向 = 方向.向右;
            info_1.dic固定转换.Add(方向.向右, new List<方向>{ 方向.向右, 方向.向下 });
            info_1.dic固定转换.Add(方向.向下, new List<方向>{ 方向.向下, 方向.向左 });
            info_1.dic固定转换.Add(方向.向左, new List<方向>{ 方向.向左, 方向.向上 });
            info_1.dic固定转换.Add(方向.向上, new List<方向>{ 方向.向上, 方向.向右 });
            csPrintNum cs1 = new csPrintNum(n, info_1);
            cs1.PrintMatrix();

            ChangeInfo info_2 = new ChangeInfo();
            info_2.起始方向 = 方向.向右;
            info_2.dic固定转换.Add(方向.向右, new List<方向> { 方向.向左下, 方向.向右上 });
            info_2.dic固定转换.Add(方向.向下, new List<方向> { 方向.向右上, 方向.向左下 });
            info_2.dic固定转换.Add(方向.向左下, new List<方向>{ 方向.向左下, 方向.向下, 方向.向右 });
            info_2.dic固定转换.Add(方向.向右上, new List<方向>{ 方向.向右上, 方向.向右, 方向.向下 });
            csPrintNum cs2 = new csPrintNum(8, info_2);
            cs2.PrintMatrix();

            ChangeInfo info_3 = new ChangeInfo();
            info_3.dic固定转换.Add(方向.无, new List<方向>{ 方向.向左下 });
            info_3.dic固定转换.Add(方向.向左下, new List<方向>{ 方向.向左下 });
            for (int i = 1; i < n; i++)
                info_3.ls离散起始点.Add(new Point(i, 0));
            for (int i = 1; i < n; i++)
                info_3.ls离散起始点.Add(new Point(n - 1, i));
            csPrintNum cs3 = new csPrintNum(n, info_3);
            cs3.PrintMatrix();

            ChangeInfo info_4 = new ChangeInfo();
            info_4.dic固定转换.Add(方向.无, new List<方向>{ 方向.向右下 });
            info_4.dic固定转换.Add(方向.向右下, new List<方向>{ 方向.向右下 });
            for (int i = 1; i < n; i++)
            {
                info_4.ls离散起始点.Add(new Point(i, 0));
                info_4.ls离散起始点.Add(new Point(0, i));
            }
            csPrintNum cs4 = new csPrintNum(n, info_4);
            cs4.PrintMatrix();

            ChangeInfo info_5 = new ChangeInfo();
            info_5.i起始点值 = 64;
            info_5.i变化值 = -1;
            info_5.起始方向 = 方向.向右;
            info_5.dic固定转换.Add(方向.向右, new List<方向>{ 方向.向右, 方向.向下 });
            info_5.dic固定转换.Add(方向.向下, new List<方向>{ 方向.向右, 方向.向下, 方向.向左 });
            info_5.dic固定转换.Add(方向.向左, new List<方向>{ 方向.向上, 方向.向左, 方向.向下 });
            info_5.dic固定转换.Add(方向.向上, new List<方向>{ 方向.向上, 方向.向左 });
            csPrintNum cs5 = new csPrintNum(n, info_5);
            cs5.PrintMatrix();

            ChangeInfo info_6 = new ChangeInfo();
            info_6.起始方向 = 方向.向右;
            info_6.dic固定转换.Add(方向.向右, new List<方向>{ 方向.向右, 方向.向下 });
            info_6.dic固定转换.Add(方向.向下, new List<方向>{ 方向.向右, 方向.向左 });
            info_6.dic固定转换.Add(方向.向左, new List<方向>{ 方向.向左, 方向.向下 });
            csPrintNum cs6 = new csPrintNum(n, info_6);
            cs6.PrintMatrix();
        }
    }
}

#50


引用 47 楼 fckadxz 的回复:
花了几个小时,6个图都比较圆满地解决了.
可读行和扩展性 马马虎虎,呵呵.不废话,上code.
[img=http://p.blog.csdn.net/images/p_blog_csdn_net/renrousousuo/42.gif]图[/img] 写得挺精彩,一贴搞定是我期待的!

不过有点问题,当N=9的时候,输出:
详情
01 02 03 04 05 06 07 08 09
32 33 34 35 36 37 38 39 10
31 56 57 58 59 60 61 40 11
30 55 72 73 74 75 62 41 12
29 54 71 80 81 76 63 42 13
28 53 70 79 78 77 64 43 14
27 52 69 68 67 66 65 44 15
26 51 50 49 48 47 46 45 16
25 24 23 22 21 20 19 18 17
-----------
01 02 06 07 15 16 28 29
03 05 08 14 17 27 30 43
04 09 13 18 26 31 42 44
10 12 19 25 32 41 45 54
11 20 24 33 40 46 53 55
21 23 34 39 47 52 56 61
22 35 38 48 51 57 60 62
36 37 49 50 58 59 63 64
-----------
01 02 04 07 11 16 22 29 37
03 05 08 12 17 23 30 38 46
06 09 13 18 24 31 39 47 54
10 14 19 25 32 40 48 55 61
15 20 26 33 41 49 56 62 67
21 27 34 42 50 57 63 68 72
28 35 43 51 58 64 69 73 76
36 44 52 59 65 70 74 77 79
45 53 60 66 71 75 78 80 81
-----------
01 10 26 40 52 62 70 76 80
18 02 11 27 41 53 63 71 77
33 19 03 12 28 42 54 64 72
46 34 20 04 13 29 43 55 65
57 47 35 21 05 14 30 44 56
66 58 48 36 22 06 15 31 45
73 67 59 49 37 23 07 16 32
78 74 68 60 50 38 24 08 17
81 79 75 69 61 51 39 25 09
-----------
64 63 62 61 60 59 58 57 56
33 34 35 36 37 38 39 40 55
32 31 30 29 28 27 26 41 54
09 10 11 12 13 14 25 42 53
08 07 06 05 04 15 24 43 52
-07 -06 -05 -04 03 16 23 44 51
-08 -09 -10 -03 02 17 22 45 50
-15 -14 -11 -02 01 18 21 46 49
-16 -13 -12 -01 00 19 20 47 48
-----------
01 02 03 04 05 06 07 08 09
18 17 16 15 14 13 12 11 10
19 20 21 22 23 24 25 26 27
36 35 34 33 32 31 30 29 28
37 38 39 40 41 42 43 44 45
54 53 52 51 50 49 48 47 46
55 56 57 58 59 60 61 62 63
72 71 70 69 68 67 66 65 64
73 74 75 76 77 78 79 80 81
-----------

起始点可以用边界来代表,谢谢参与!