博客园不常在线,有问题联系微信,微信:17873041739


C#数据结构与算法系列(二):稀疏数组(SparseArray)

1.介绍
当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组。
  
稀疏数组的处理方法是:  

1.记录数组一共有几行几列,有多少个不同的值
  
2.把具有不同值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模

 

 2.分析

 

 

3.代码实现

    public class SparseArray
    {
        public static void Test()
        {
            int[][] chessArr = new int[11][];

            //
            int sum = 0;

            Console.WriteLine("\n初始化棋盘");
            //初始化棋盘
            for (int i = 0; i < chessArr.Length; i++)
            {
                chessArr[i] = new int[11];


                for (int j = 0; j < chessArr[i].Length; j++)
                {

                    if (i == 1 && j == 2)
                    {
                        chessArr[i][j] = 1;

                        sum++;
                    }

                    if (i == 2 && j == 3)
                    {
                        chessArr[i][j] = 2;

                        sum++;
                    }

                    Console.Write(chessArr[i][j] + "\t");
                }

                Console.WriteLine();
            }

            Console.WriteLine("\n初始化非零的个数:"+sum);

            //创建对应的稀疏数组
            int[][] sparseArr =new int[3][];

            for (int i = 0; i < sparseArr.Length; i++)
            {
                sparseArr[i] = new int[3];
            }

            sparseArr[0][0] = 11;

            sparseArr[0][1] = 11;

            sparseArr[0][2] = sum;

            int count = 0;

            for (int i = 0; i < chessArr.Length; i++)
            {
                for (int j = 0; j < chessArr[i].Length; j++)
                {
                    if (chessArr[i][j]!=0)
                    {
                        count++;
                        sparseArr[count][0] = i;
                        sparseArr[count][1] = j;
                        sparseArr[count][2] = chessArr[i][j];
                    }
                }
            }

            Console.WriteLine("\n稀疏数组的形式:");

            foreach (var row in sparseArr)
            {
                foreach (var data in row)
                {
                    Console.Write($"{data}\t");
                }

                Console.WriteLine();
            }

            int[][] newChessArr = new int[sparseArr[0][0]][];

            for (int i = 0; i < newChessArr.Length; i++)
            {
                newChessArr[i] = new int[sparseArr[0][1]];
            }

           
            for (int i = 1; i < sparseArr.Length; i++)
            {
                newChessArr[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
            }

            Console.WriteLine("\n还原后棋盘:");
            foreach (var row in newChessArr)
            {
                foreach (var data in row)
                {
                    Console.Write($"{data}\t");
                }

                Console.WriteLine();
            }
        }
    }

 4.效果图

 

posted @ 2020-06-13 13:17  Code技术分享  Views(551)  Comments(0Edit  收藏  举报

博客园不常在线,有问题联系微信,微信:17873041739