unity游戏开发-C#语言基础篇(数组排序和倒序)

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("第一题");
            int[] arrA = { 1, 3, 4, 5, 6 };
            string[] arrB = { "7", "8", "9" };
            int[] arrC = new int[arrA.Length + arrB.Length];

            for (int i = 0; i < arrA.Length + arrB.Length; i++)
            {

                if (i < arrA.Length)
                {
                    arrC[i] = arrA[i];
                }
                else
                {
                    arrC[i] = Convert.ToInt32(arrB[i - arrA.Length]);
                }
                // Console.Write(arrC[i]);

            }

            foreach (int arrC1 in arrC)
            {
                Console.WriteLine(arrC1);
            }



            Console.WriteLine("\n\n第二题");

            int[] arrD = new int[19];
            int j = 0;
            for (int i = 0; i < arrD.Length; i++)
            {
                if (i % 2 == 0 && j < arrC.Length)
                {
                    arrD[i] = arrC[arrC.Length - 1 - j];
                    j++;
                }
            }

            foreach (int arrD1 in arrD)
            {
                Console.Write(arrD1 + " ");
            }

            //练
            //-------第一题----------------数组相加
            int[] arrA = { 1, 3, 4, 5, 6 };
            string[] arrB = { "7", "8", "9" };


            int[] arrC = new int[arrA.Length + arrB.Length];//降低藕合性;
            for (int i = 0; i < arrC.Length; i++)
            {
                if (i < arrA.Length)//判断下标小于A数组,直接赋值
                {
                    arrC[i] = arrA[i];
                }
                else
                {//判断下标超过A数组后,强转成整型;注意数组B的下标,要从0开始;
                    arrC[i] = Convert.ToInt32(arrB[i - arrA.Length]);
                }
                // Console.Write(arrC[i]+" ");

            }

            foreach (int arrC1 in arrC)
            {
                Console.Write(arrC1 + " ");
            }
            Console.Write("\n\n");


            //-------第二题----------------偶数位倒序

            int[] arrD = new int[19];//固定数组D的长度;
            int index = arrC.Length - 1;//定义数组C的长度赋值;
            for (int i = 0; i < arrD.Length; i++)
            {
                if (i % 2 == 0 && index >= 0)
                {//判断偶数位;注意下标不能取到负数;
                    arrD[i] = arrC[index];//倒序下标
                    index--;//下标迭代递减;注意不能小于0;否则溢出;
                }
            }
            foreach (int arrD1 in arrD)
            {
                Console.Write(arrD1 + " ");
            }



            //----------二维数组-----------
            //练1
            int[,] arr = new int[3, 4] { { 0, 1, 2, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    Console.WriteLine("arr[{0},{1}]={2}", i, j, arr[i, j]);
                }
                Console.WriteLine("\n");
            }

            //练2
            int[,] arr = new int[2, 2] { { 1, 2 }, { 3, 4 } };
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    Console.WriteLine(arr[i, j]);
                }
            }

            //练习
            //第一题 最大排前面 最小排后面

            int[] Arr = new int[10];
            // int shuru =Convert.ToInt32( Console.ReadLine());
            Console.WriteLine("请输入十个数,从小排到大 ");
            for (int i = 0; i < Arr.Length; i++)
            {
                Arr[i] = Convert.ToInt32(Console.ReadLine());
            }
            int arrMax = Arr[0];
            int arrMin = Arr[0];
            for (int i = 0; i < Arr.Length; i++)
            {
                if (arrMax < Arr[i])
                {
                    arrMax = Arr[i];
                }
                if (arrMin > Arr[i])
                {
                    arrMin = Arr[i];
                }
            }
            //int[] arrF=new int[Arr.Length];
            for (int i = 0; i < Arr.Length; i++)
            {
                if (Arr[i] == arrMax)
                {
                    int indexMax = i;
                    int temp = Arr[0];
                    Arr[0] = Arr[indexMax];
                    Arr[indexMax] = temp;
                }
                if (Arr[i] == arrMin)
                {
                    int indexMin = i;
                    int temp1 = Arr[Arr.Length - 1];
                    Arr[Arr.Length - 1] = Arr[indexMin];
                    Arr[indexMin] = temp1;
                }
            }

            Console.Write("\n最大最小 ");
            foreach (int Arr1 in Arr)
            {
                Console.Write(Arr1 + " ");
            }






            //倒序
            for (int i = 0; i < Arr.Length; i++)
            {
                for (int j = i; j < Arr.Length; j++)
                {
                    if (Arr[i] > Arr[j])
                    {
                        int temp = Arr[i];
                        Arr[i] = Arr[j];
                        Arr[j] = temp;
                    }
                }
            }


            Console.Write("\n排序后 ");
            foreach (int Arr2 in Arr)
            {
                Console.Write(Arr2 + " ");
            }


            Console.ReadKey();
        
        }
    }

你可能感兴趣的:(unity游戏开发-C#语言基础篇(数组排序和倒序))