猿问

我想通过将输入数组的所有项目相乘来计算,但除了第 i 个项目

举个例子


给定以下数组,inputArray: [2,3,6,8]

结果数组将是: resultArray: [144,96,48,36]


resultArray[0] = inputArray[1] * inputArray[2] * inputArray[3]

resultArray[1] = inputArray[0] * inputArray[2] * inputArray[3]

resultArray[n] = inputArray[0] *...* inputArray[n-1] * inputArray[n+1] *...* inputArray[last]

我已经编写了如下代码,但是如何使用 for 或其他循环进行此计算。


static void multiply()

    {

        int[] inputArray = { 2, 3, 6, 8 };

        int[] resultArray = { 1, 1, 1, 1 };

        for (int i = 0; i < inputArray.Length; i++)

        {

            Console.Write(inputArray[i] + " ");

        }

        Console.WriteLine();

        //for (int i = 0; i < inputArray.Length; i++)

        //{

        //    resultArray[i] = inputArray[0] * inputArray[i - 1] * inputArray[i + 1];

        //}

        resultArray[0] = inputArray[1] * inputArray[2] * inputArray[3];

        resultArray[1] = inputArray[0] * inputArray[2] * inputArray[3];

        resultArray[2] = inputArray[0] * inputArray[1] * inputArray[3];

        resultArray[3] = inputArray[0] * inputArray[1] * inputArray[2];


        for (int i = 0; i < resultArray.Length; i++)

        {

            Console.Write(resultArray[i] + " ");

        }

    }


慕容森
浏览 160回答 3
3回答

拉丁的传说

我的方法遵循许多人的思路。为了简单起见,我使用了 LINQ。&nbsp; &nbsp; &nbsp; &nbsp; int fullProduct = 1;&nbsp; &nbsp; &nbsp; &nbsp; List<int> input = new List<int> { 2, 3, 6, 8 };&nbsp; &nbsp; &nbsp; &nbsp; List<int> result = new List<int>();&nbsp; &nbsp; &nbsp; &nbsp; input.ForEach(v => { fullProduct *= v; });&nbsp; &nbsp; &nbsp; &nbsp; input.ForEach(c=>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result.Add(fullProduct / c);&nbsp; &nbsp; &nbsp; &nbsp; });

子衿沉夜

使用 Linq 执行此操作的一种方法是使用Aggregate获取值的总乘积,然后将结果值分配为等于乘积除以当前循环索引处的输入项:static void Multiply(){&nbsp; &nbsp; int[] input = { 2, 3, 6, 8 };&nbsp; &nbsp; int[] result = new int[input.Length];&nbsp; &nbsp; var product = input.Aggregate((i, j) => i * j);&nbsp; &nbsp; for (int i = 0; i < input.Length; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; result[i] = product / input[i];&nbsp; &nbsp; }&nbsp; &nbsp; Console.WriteLine(string.Join(" ", input));&nbsp; &nbsp; Console.WriteLine(string.Join(" ", result));&nbsp; &nbsp; Console.ReadKey();}输出

德玛西亚99

class Program{&nbsp; &nbsp; static int GetMulResult(int[] input, int ommitingIndex)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int result = 1;&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < input.Length; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i == ommitingIndex)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result *= input[i];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; }&nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int[] inputArray = { 2, 3, 6, 8 };&nbsp; &nbsp; &nbsp; &nbsp; int[] result1 = new int[4];&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < inputArray.Length; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result1[i] = GetMulResult(inputArray, i);&nbsp; &nbsp; }}PS。恐怕如果你不能创建这样一个简单的算法,你将无法创建更多可用的算法。你应该为此努力。
随时随地看视频慕课网APP
我要回答