LeetCode 628 三个数的最大乘积 数组 c#

 
 
 
 
这道题我想的是先找到最大的三个数在数组中,但是难度很大,索性借用冒泡排序将三个数值排序一下计算就容易多了。

所有程序都是在Visual Studio软件上运行测试成功后copy过来的-----答案如下:

class Program
    {
        static void Main(string[] args)
        {
            int sums = 1;
            int[] tes = new int[] { 2, 1, 6, 4, 5 };
            int[] strss= Get(tes);
            for (int i = strss.Length-1; i > 1; i--)
            {
                sums *= strss[i];
            }
            Console.WriteLine(sums);
            Console.ReadKey();  
        }

        private static int[] Get(int[] nums)
        {
            //首先,记录最大的三个数的位置,但是要定义三个变量存储最大值。
            //倒不如进行排序,然后计算前三个数值
           int [] strinput= Getarr(nums);
 
           return nums; 
        }

        private static int[] Getarr(int[] data)
        {
            for (int i = 0; i < data.Length - 1; i++)
            {
                for (int j = 0; j < data.Length - 1 - i; j++)
                {
                    if (data[j] > data[j + 1])
                    {
                        data[j] = data[j] + data[j + 1];
                        data[j + 1] = data[j] - data[j + 1];
                        data[j] = data[j] - data[j + 1];
                    }
                }
            }
            return data;
        }
        
    }

 
 

  以上程序大量的篇幅写了关于排序的功能,是时候思考几个问题了:

1、已经有了排序数组的函数:

将上面的程序替换一下:

            int[] intArr = new int[] { 2, 1, 6, 4, 5 };             Array.Sort(intArr); //正序

            Array.Reverse(intArr);//反序

2、我看了一下程序,没有什么多余的了,兄die.



猜你喜欢

转载自blog.csdn.net/us2019/article/details/80208801