| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2512 人关注过本帖
标题:C#编写螺旋矩阵!給个讲解细致的!控制台应用程序!
只看楼主 加入收藏
一一一
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2009-7-21
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
C#编写螺旋矩阵!給个讲解细致的!控制台应用程序!
C#编写螺旋矩阵!給个讲解细致的!控制台应用程序!
5*5

运行大概是这样:
0 1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
搜索更多相关主题的帖子: 讲解 控制台 矩阵 螺旋 
2009-07-22 10:34
乖老鼠
Rank: 5Rank: 5
来 自:四川省
等 级:职业侠客
威 望:2
帖 子:434
专家分:394
注 册:2008-9-8
收藏
得分:5 
在网上找到一篇 LZ应该问之前自己搜一下
原址:http://hi.baidu.com/woiwojia/blog/item/ba397800a9f91d15738b6526.html

输出螺旋矩阵[c#]2008-03-18 22:52例如输出4*4的螺旋矩阵:
1   12   11   10
2   13   16    9
3   14   15    8
4    5     6     7

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = 0;//螺旋方阵的阶数
            bool inputIsRight = false; //输入是否正确
            do
            {   Console.Write("请输入螺旋方阵的阶数(大于零的整数):");
                try
                {
                    string iStr = Console.ReadLine();
                    n = Convert.ToInt16(iStr);
                    if (n < 1)
                        inputIsRight = true;
                    else
                        inputIsRight = false;
                }
                catch
                {
                    Console.Write("输入有误\n" + "请重新输入螺旋方阵的阶数(大于零的整数):");
                }
            } while (inputIsRight);
            int [,]iArray = new int[n,n];
            int rc = 0, iValue = 1;    //rc=0:竖着往下走给数组赋值,rc=1:横着往右走给数组赋值,
                                       //rc=2:竖着往上走给数组赋值,rc=3:横着往左走给数组赋值
            int i=-1,j=0;
            int maxRow = n, maxCol = n, minRow = -1, minCol = -1;
            do
            {
                switch (rc)
                {
                    case 0:
                        i++; //防止如iVarray[0,1]这样的拐点重复赋值
                        while (i < maxRow )
                        {
                            iArray[i, j] = iValue++;
                            i++;
                        }
                        i--;//一步完后i=maxRow(溢出),所以要i--
                        minCol++;
                        rc = 1;
                        break;
                    case 1:
                        j++;
                        while (j < maxCol)
                        {
                            iArray[i, j] = iValue++;
                            j ++;
                        }
                        j--; ;
                        maxRow--;
                        rc = 2;
                        break;
                    case 2:
                        i--;
                        while (i > minRow)
                        {
                            iArray[i, j] = iValue++;
                            i--;
                        }
                        i++;
                        maxCol--;
                        rc = 3;
                        break;
                    case 3:
                        j--;
                        while (j >minCol)
                        {
                            iArray[i, j] = iValue++;
                            j--;
                        }
                        j++; ;
                        minRow++;
                        rc = 0;
                        break;
                }
            }while(iValue<=n*n);
            int cout = 0;
            foreach (int ii in iArray) //输出矩阵
            {
                if (ii < 10)
                    Console.Write(" " + ii.ToString() + " ");
                else if (ii < 100)
                    Console.Write(" " + ii.ToString() + " ");
                else
                    Console.Write(ii.ToString() + " ");
                cout++;
                if (cout % n == 0) Console.WriteLine();
            }
        }
    }
}

[[it] 本帖最后由 乖老鼠 于 2009-7-22 11:35 编辑 [/it]]

转眼就从编程菜鸟混成了半灌水
2009-07-22 11:07
NTYLWJ
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:191
专家分:698
注 册:2008-12-2
收藏
得分:5 
还真不会了。得高手解决中。
2009-07-22 11:27
czh2076
Rank: 1
等 级:新手上路
帖 子:8
专家分:6
注 册:2009-7-20
收藏
得分:5 
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] array = new int[5,5];  //定义一个二维数组,new命令用于开辟空间
            int count=1;                  //
            for (int i = 0; i < 5; i++)   //用二维数组进行遍历赋值
            {
                for (int j = 0; j < 5; j++)
                {
                    array[i, j] = count++;
                }
            }
            for (int i = 0; i < 5; i++)   //用二维数组进行遍历输出
            {
                for (int j = 0; j < 5; j++)
                {
                    Console.Write(" " + array[i, j]);
                }
                Console.WriteLine();     //用于控制换行
            }
        }
    }
}
2009-07-22 12:44
baikil
Rank: 10Rank: 10Rank: 10
来 自:X星球
等 级:青峰侠
威 望:4
帖 子:282
专家分:1580
注 册:2009-7-20
收藏
得分:5 
试试这个吧,写的不好,应该够你用不同宽度的螺旋矩阵的:
        static void Main(string[] args)
        {
            int n = 0;
            while (true)
            {
                Console.Write("请输入矩阵的宽度:");
                string s = Console.ReadLine();

                if (int.TryParse(s, out n))
                    break;
            }
            byte[,] bs = new byte[n, n];    //存放螺旋的数据
            byte bi=0;                      //螺旋的初时值
            int temN=n;
            for (int c = 0; c < n / 2f;c++,temN-=2) //对每一层螺旋的去壳
            {
                if (temN > 1)
                {
                    //上一行
                    for (int i = 0; i < temN-1; i++)
                    {
                        bs[c, i+c] = bi;
                        bi++;
                    }
                    //右一例
                    for (int i = 0; i < temN-1; i++)
                    {
                        bs[i+c, temN - 1+c] = bi;
                        bi++;
                    }
                    //下一行
                    for (int i = temN; i > 1; i--)
                    {
                        bs[temN - 1+c, i - 1+c] = bi;
                        bi++;
                    }
                    //左一列
                    for (int i = temN; i > 1; i--)
                    {
                        bs[i - 1+c, c] = bi;
                        bi++;
                    }
                }
                else
                {
                    //螺旋为单数时最中间一位数
                    bs[c, c] = bi;
                }
            }
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (bs[i, j] > 9)
                        Console.Write(bs[i, j] + " ");
                    else
                        Console.Write(bs[i, j] + "  ");
                }
                Console.Write("\n");
            }
            Console.ReadLine();
        }

[[it] 本帖最后由 baikil 于 2009-7-23 14:30 编辑 [/it]]

自创QQ群64885635,方便C#学习交流.希望各项工作路好学都好入!
2009-07-23 14:27
快速回复:C#编写螺旋矩阵!給个讲解细致的!控制台应用程序!
数据加载中...
 
   



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

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