Unity3D开发-C#语言进阶篇(冒泡排序最大值最小值以及对角线之和)

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("----------第一题------------");
            int[,] arr = {
                {26,23,156,25 },
                {11,833,52,117 },
                { 114,33,542,17},
            };
            //把二维数组变成一维数组
            int[] arr1 = new int[arr.Length];//一维数组长度为二维数组的长度
            int index = 0;//定义下标
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    arr1[index] = arr[i, j];//赋值;
                    index++;//叠加
                }

            }
            //寻找最大值最小值
            int arrMax = arr1[0];
            int arrMin = arr1[0];
            for (int i = 0; i < arr1.Length; i++)
            {
                //找出最大值
                if (arrMax < arr1[i])
                {
                    arrMax = arr1[i];
                }
                //找出最小值
                if (arrMin > arr1[i])
                {
                    arrMin = arr1[i];
                }
            }
            Console.WriteLine("\n最大值为:{0}\n最小值为:{1}\n", arrMax, arrMin);

            //排序,
            for (int i = 0; i < arr1.Length; i++)
            {
                for (int j = i + 1; j < arr1.Length; j++)
                {
                    if (arr1[i] > arr1[j])
                    {//从小排到大;
                        int temp = arr1[i];
                        arr1[i] = arr1[j];
                        arr1[j] = temp;
                    }
                }

            }

            index = 0;//排完之后下标归零;重复利用;
            //将一维数组变成二维数组;
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    arr[i, j] = arr1[index];//二维数组赋值;
                    index++;//下标叠加;
                }

            }
            Console.WriteLine("\n排序后:\n");
            index = 0;//重新归零.换行用
            foreach (int arr2 in arr)
            {//遍历数组
                Console.Write(arr2 + " ");
                index++;//叠加;
                if (index % 4 == 0)
                {//换行
                    Console.WriteLine();
                }

            }





            Console.WriteLine("\n----------第二题------------");
            //任意输入9个数,把这9个数存到一个3行3列的二维数组中,
            //要求横向递增,纵向递增;
            //另外求出他们对角线位置的数之和
            int[] arr_1 = new int[9];//定义长度;           
            for (int i = 0; i < arr_1.Length; i++)
            {
                Console.WriteLine("请输入第{0}数:", i + 1);//输入9个数;
                arr_1[i] = Convert.ToInt32(Console.ReadLine());//字符串强转整型;

            }

            //排序
            for (int i = 0; i < arr_1.Length; i++)
            {
                for (int j = i + 1; j < arr_1.Length; j++)
                {
                    if (arr_1[i] > arr_1[j])
                    {
                        int temp_1 = arr_1[i];
                        arr_1[i] = arr_1[j];
                        arr_1[j] = temp_1;
                    }
                }
            }

            //一维数组变成二维数组;
            int index_1 = 0;
            int[,] arr_2 = new int[3, 3];
            int sum = 0;
            Console.WriteLine("\n排序后的数组为:\n");
            for (int i = 0; i < arr_2.GetLength(0); i++)
            {
                for (int j = 0; j < arr_2.GetLength(1); j++)
                {
                    arr_2[i, j] = arr_1[index_1];
                    index_1++;
                    if (i == j || i + j == 2)
                    {
                        sum += arr_2[i, j];
                    }
                    Console.Write(arr_2[i, j] + " ");
                }
                Console.WriteLine();
            }

            Console.WriteLine("\n对角线之和为:{0}\n", sum);




            Console.ReadKey();
        }
    }

你可能感兴趣的:(Unity3D开发-C#语言进阶篇(冒泡排序最大值最小值以及对角线之和))