c#之数字回旋矩阵

 

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

namespace Myjuzheng
{
    class Program
    {
        static  int[,] shuzu;
        static void Main(string[] args)
        {
            shuzu = new int[20, 20];
            int i=0;
            int j=0;
            shuzu[0, 0] = 1;
            for (int num = 2; num <=shuzu.Length ; num++)
   {
                if (panduan(i, j + 1) && !panduan(i - 1, j))
                {
                    shuzu[i, j + 1] = num;
                    j++;
                    continue;
                }
                if (panduan(i + 1, j) && !panduan(i, j + 1))
                {
                    shuzu[i + 1, j] = num;
                    i++;
                    continue;

                }
                if (panduan(i, j - 1) && !panduan(i + 1, j))
                {
                    shuzu[i, j-1] = num;
                    j--;
                    continue;

                }
                if (panduan(i - 1, j) && !panduan(i, j - 1))
                {
                    shuzu[i - 1, j] = num;
                    i--;
                    continue;

                }

               
   }
            for (int m= 0; m < shuzu.GetLength(0); m++)
            {
                for (int n = 0; n <shuzu.GetLength(0); n++)
                {
                    Console.Write("{0,4}", shuzu[m, n]);
                }
                Console.WriteLine();
            }
            Console.Read();
 
        }

        static bool panduan(int x, int y)
        {
            if (x < 0 || x >= shuzu.GetLength(0) || y < 0 || y >= shuzu.GetLength(0))
                return false;
            if (shuzu[x, y] != 0)
                return false;

            return true;
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值