| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 686 人关注过本帖, 1 人收藏
标题:很棘手的问题
只看楼主 加入收藏
hanxing15
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2011-6-2
结帖率:83.33%
收藏(1)
已结贴  问题点数:10 回复次数:4 
很棘手的问题
将将从1到n*n的n的平方个数,按顺时针方向从小到大排列成阵列,怎么做?
如n=4则效果图:
1, 2,  3,4
12,13,14,5
11,16,15,6
10,9, 8, 7

[ 本帖最后由 hanxing15 于 2011-10-28 09:49 编辑 ]
搜索更多相关主题的帖子: 效果图 顺时针 
2011-10-28 09:48
hanxing15
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2011-6-2
收藏
得分:0 
没人会吗
2011-10-28 10:40
qubo1982
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:367
专家分:1132
注 册:2009-3-18
收藏
得分:10 
程序代码:
以前就有人要问过了。再给你贴一次

Console.WriteLine("Input:");
            int n = int.Parse(Console.ReadLine());
            int[,] result = new int[n,n];

            int no = 1, direction = 0,x = 0,y=0;

            for (int i = 0; i < n * n; i++)
            {
                result[x, y] = no;
                if (direction == 0)
                {
                    if (y +1 == n || result[x,y+1]!=0)
                    {
                        direction = 1;
                        x++;
                    }
                    else
                        y++;
                }
                else if (direction == 1)
                {
                    if (x + 1 == n || result[x+1,y]!= 0)
                    {
                        direction = 2;
                        y--;
                    }
                    else
                        x++;
                }
                else if (direction == 2)
                {
                    if (y == 0 || result[x, y - 1] != 0)
                    {
                        direction = 3;
                        x--;
                    }
                    else
                        y--;
                }
                else if (direction == 3)
                {
                    if (result[x - 1, y] != 0)
                    {
                        direction = 0;
                        y++;
                    }
                    else
                        x--;
                }
                no++;
            }

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    Console.Write(result[i, j] + "\t");
                }
                Console.WriteLine();
            }
            Console.ReadLine();
2011-10-28 10:59
clhc
Rank: 6Rank: 6
等 级:侠之大者
帖 子:69
专家分:441
注 册:2011-10-11
收藏
得分:0 
程序代码:
    class Program
    {
        static int[,] Map;
        static int Width;
        static int Max;

        static void InitMap(int n)
        {
            Width = n;
            Max = n * n;
            Map = new int[n, n];
        }

        static void DrawMap(int i, int j, int k, int val)
        {
            if (val > Max) return;

            Map[i, j] = val++;
            switch (k)
            {
                case 0://向右
                    j++;
                    if (j >= Width || Map[i, j] != 0)
                    {
                        i += 1;
                        j -= 1;
                        k = 1;
                    }
                    break;
                case 1://向下
                    i++;
                    if (i >= Width || Map[i, j] != 0)
                    {
                        i -= 1;
                        j -= 1;
                        k = 2;
                    }
                    break;
                case 2://向左
                    j--;
                    if (j < 0 || Map[i, j] != 0)
                    {
                        i -= 1;
                        j += 1;
                        k = 3;
                    }
                    break;
                case 3://向上
                    i--;
                    if (i < 0 || Map[i, j] != 0)
                    {
                        i += 1;
                        j += 1;
                        k = 0;
                    }
                    break;
            }

            DrawMap(i, j, k, val);
        }

        static void PrintMap()
        {
            for (int i = 0; i < Map.GetLength(0); i++)
            {
                for (int j = 0; j < Map.GetLength(1); j++)
                {
                    Console.Write(Map[i, j] + "\t");
                }
                Console.Write("\n");
            }
        }

        static void Handler(int n)
        {
            InitMap(n);
            DrawMap(0, 0, 0, 1);
            PrintMap();
        }

        static void Main(string[] args)
        {
            int n = 1;
            string input = "";

            while (true)
            {
                Console.Write("Please input n, 1 <= n <= 10, q to exit:");
                input = Console.ReadLine();

                if (input == "q") break;

                if (Int32.TryParse(input, out n) && n > 0 && n < 11)
                    Handler(n);
                else
                    Console.WriteLine(input + " is not a right integer.");

            }
        }

    }
2011-10-28 11:34
serious
Rank: 6Rank: 6
等 级:侠之大者
威 望:1
帖 子:81
专家分:497
注 册:2009-8-18
收藏
得分:0 
一个不同的方法:
程序代码:
static void Display(int[,] a)
{
    for (int i = 0; i < a.GetLength(0); ++i)
    {
        for (int j = 0; j < a.GetLength(1); ++j)
        {
            Console.Write("{0}{1}\t", a[i, j], j < a.GetLength(1) - 1 ? "," : "");
        }

        Console.WriteLine();
    }
}

static int[,] Get(int n)
{
    var m = new int[n, n];

    Get(m, 1, 0, 0, n);

    return m;
}

static void Get(int[,] m, int r, int i, int j, int n)
{
    if (n > 0)
    {
        if (n > 1)
        {
            for (int x = 0; x < n - 1; ++x)
            {
                m[i, j + x] = r + x;
                m[i + x, j + n - 1] = r + n - 1 + x;
                m[i + n - 1, j + 1 + x] = r + 3 * n - 3 - 1 - x;
                m[i + 1 + x, j] = r + 4 * n - 4 - 1 - x;
            }

            Get(m, r + 4 * n - 4, i + 1, j + 1, n - 2);
        }
        else
        {
            m[i, j] = r;
        }
    }
}

[STAThread]
static void Main(string[] args)
{
    var m = Get(1);
    Display(m);
    Console.WriteLine("==========");
    m = Get(2);
    Display(m);
    Console.WriteLine("==========");
    m = Get(3);
    Display(m);
    Console.WriteLine("==========");
    m = Get(4);
    Display(m);
    Console.WriteLine("==========");
    m = Get(5);
    Display(m);
    Console.WriteLine("==========");
    m = Get(6);
    Display(m);
    Console.WriteLine("==========");
    Console.ReadLine();
}

2011-10-29 06:08
快速回复:很棘手的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019879 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved