5*5矩陣

來源:互聯網
上載者:User

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();
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.