5*5矩阵

using System;
using System.Collections.Generic;
using System.Text;
using suanfa;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Array target_list1 = new int[10] { 2, 3, 5, 13, 21, 46, 89, 93, 2157, 5769 };
            //SortDome target = new SortDome(target_list1);
            //int actual;
            //int sum = 0;
            //int total = 0;
            //Random random = new Random(1);
            //while (sum < 10)
            //{
            //    int iNum = 0;
              
            //    iNum = random.Next(0, 6000);

            //    actual = target.Find(iNum, target.ArrayList);
            //    if (actual > 0)
            //    {
            //        sum++;
            //    }
            //    total++;
            //    Console.WriteLine(string.Format("第{3}次 随即生成 {0} 对比值 {1} 目前找到{2} 次", iNum, actual, sum, total));
            //}

            JuZhen();
            Console.ReadLine();
        }

        public static void Test()
        {
            int a, b, c;
            a = b = c = 1;
            string str = "1,1";
            for (int i = 0; i < 20; i++)
            {
                c = a + b;
                str +=","+ c.ToString();
                Console.WriteLine(str);
                a = b;
                b = c;
            }
        }

        public static void Test1()
        {
            for (int i = 1; i < 10; i++)
            {
                StringBuilder sb = new StringBuilder();
                for (int j = 1; j <= i; j++)
                {
                    sb.AppendFormat("{0}*{1}={2} ", i, j, i * j);
                }
                Console.WriteLine(sb.ToString());
            }
        }

        public static void JuZhen1()
        {
            int flag = 0;
            int nextX, nextY, num;
            int[,] arr = null;
            num = int.Parse(System.Console.ReadLine());
            arr = new int[num, num];
            nextX = nextY = 0;
            for (int i = 1; i <= num * num; i++)
            {
                arr[nextX, nextY] = i;

                switch (flag)
                {
                    case 0://you
                        if (nextY == num - 1 || arr[nextX, nextY + 1] != 0)
                        {
                            nextX++;
                            flag = (flag + 1) % 4;
                            break;
                        }
                        else
                        {
                            nextY++;
                            break;
                        }
                    case 1://xia
                        if (nextX == num - 1 || arr[nextX + 1, nextY] != 0)
                        {
                            nextY--;
                            flag = (flag + 1) % 4;
                            break;
                        }
                        else
                        {
                            nextX++;
                            break;
                        }
                    case 2://zuo
                        if (nextY == 0 || arr[nextX, nextY - 1] != 0)
                        {
                            nextX--;
                            flag = (flag + 1) % 4;
                            break;
                        }
                        else
                        {
                            nextY--;
                            break;
                        }
                    case 3://shang
                        if (nextX == 0 || arr[nextX - 1, nextY] != 0)
                        {
                            nextY++;
                            flag = (flag + 1) % 4;
                            break;
                        }
                        else
                        {
                            nextX--;
                            break;
                        }
                }
            }
            for (int i = 0; i < num; i++)
            {
                for (int j = 0; j < num; j++)
                {
                    System.Console.Write("{0} ", arr[i, j].ToString("00"));
                }
                System.Console.WriteLine();
            }
            System.Console.ReadLine();
        }

        public static void JuZhen()
        {
            int flag = 0;
            int nextX, nextY, num;
            int[,] arr = null;
            num = int.Parse(System.Console.ReadLine());
            arr = new int[num, num];
            nextX = nextY = 0;
            for (int i = 1; i <= num * num; i++)
            {
                arr[nextX, nextY] = i;

                switch (flag)
                {
                    case 0://  下
                        if (nextY == num - 1 || arr[nextX, nextY + 1] != 0)
                        {
                            nextX++;
                            flag = (flag + 1) % 4;
                            break;
                        }
                        else
                        {
                            nextY++;
                            break;
                        }
                    case 1://右
                        if (nextX == num - 1 || arr[nextX + 1, nextY] != 0)
                        {
                            nextY--;
                            flag = (flag + 1) % 4;
                            break;
                        }
                        else
                        {
                            nextX++;
                            break;
                        }
                    case 2://上
                        if (nextY == 0 || arr[nextX, nextY - 1] != 0)
                        {
                            nextX--;
                            flag = (flag + 1) % 4;
                            break;
                        }
                        else
                        {
                            nextY--;
                            break;
                        }
                    case 3://左
                        if (nextX == 0 || arr[nextX - 1, nextY] != 0)
                        {
                            nextY++;
                            flag = (flag + 1) % 4;
                            break;
                        }
                        else
                        {
                            nextX--;
                            break;
                        }
                }
            }
            for (int i = 0; i < num; i++)
            {
                for (int j = 0; j < num; j++)
                {
                    System.Console.Write("{0} ", arr[i, j].ToString("00"));
                }
                System.Console.WriteLine();
            }
            System.Console.ReadLine();
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值