【问题标题】:I want to calculated by multiplying all of the items of the input array, but except for example i-th item我想通过将输入数组的所有项目相乘来计算,但除了第 i 个项目
【发布时间】:2019-05-14 14:31:49
【问题描述】:

举个例子

给定以下数组,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] + " ");
        }
    }

【问题讨论】:

  • 为什么不将所有数字相乘,然后将该数字除以数组中第 i 个数字中的数字?
  • @Andersnk 您应该将其添加为示例的答案
  • @Andersnk 如果遇到大量数字,您的方法将失败,例如{ 1001, 1002, 1003, 1004 }.
  • @Andersnk,如果任何值为零,则将不起作用。
  • 如果提供的任何答案有帮助,礼仪是将其标记为“答案”,并可能对其有用性表示赞同。这可以防止这个问题出现在 SOF 中的“显示未回答的问题”下,并且还可以给帮助你的人更多的声誉。

标签: c# arrays .net algorithm


【解决方案1】:
class Program
{

    static int GetMulResult(int[] input, int ommitingIndex)
    {
        int result = 1;
        for(int i = 0; i < input.Length; i++)
        {
            if (i == ommitingIndex)
                continue;

            result *= input[i];
        }

        return result;
    }
    static void Main(string[] args)
    {
        int[] inputArray = { 2, 3, 6, 8 };

        int[] result1 = new int[4];


        for(int i = 0; i < inputArray.Length; i++)
            result1[i] = GetMulResult(inputArray, i);



    }
}

PS。恐怕如果你不能创建这样一个简单的算法,你将无法创建更多可用的算法。你应该努力。

【讨论】:

    【解决方案2】:

    我的方法遵循许多人的思路。为了简单起见,我使用了 LINQ。

            int fullProduct = 1;
            List<int> input = new List<int> { 2, 3, 6, 8 };
            List<int> result = new List<int>();
    
            input.ForEach(v => { fullProduct *= v; });
            input.ForEach(c=>
            {
               result.Add(fullProduct / c);
            });
    

    【讨论】:

      【解决方案3】:

      使用 Linq 的一种方法是使用 Aggregate 获得值的总乘积,然后将结果值分配为等于当前循环索引处的输入项的乘积:

      static void Multiply()
      {
          int[] input = { 2, 3, 6, 8 };
          int[] result = new int[input.Length];
      
          var product = input.Aggregate((i, j) => i * j);
      
          for (int i = 0; i < input.Length; i++)
          {
              result[i] = product / input[i];
          }
      
          Console.WriteLine(string.Join(" ", input));
          Console.WriteLine(string.Join(" ", result));
          Console.ReadKey();
      }
      

      输出

      【讨论】:

        【解决方案4】:

        如果索引相同,则不能乘以该值:

        private static void multiply()
        {
            int[] inputArray = { 2, 3, 6, 8 };
            int[] resultArray = new int[inputArray.Length];
        
            for ( int i = 0; i < inputArray.Length; i++ )
                Console.Write(inputArray[i] + " ");
        
            Console.WriteLine();
        
            for ( int indexInOutput = 0; indexInOutput < resultArray.Length; indexInOutput++ )
            {
                int result = 1;
                for ( int indexInInput = 0; indexInInput < inputArray.Length; indexInInput++ )
                {
                    if ( indexInInput != indexInOutput )
                        result *= inputArray[indexInInput];
                }
        
                resultArray[indexInOutput] = result;
            }
        
            for ( int i = 0; i < resultArray.Length; i++ )
                Console.Write(resultArray[i] + " ");
        }
        

        结果

        144 96 48 36
        

        如你所愿。

        这种方式可以避免在输入数组中有大数字时发生的溢出,并且您要尝试将所有数字相乘并除以未使用的数字,例如如果inputArray

        { 1001, 1002, 1003, 1004 }
        

        【讨论】:

          【解决方案5】:

          基本上,您只是将所需索引的数字留在结果数组中,因此您可以除以该数字。下面是可以帮助你的代码

                      int result = 1;
                              for (int j = 0; j < inputArray.Length; j++)
                              {
                                  result = result * inputArray[j];
                              }
          
                          for (int i = 0; i < resultArray.Length; i++)
                          {
          
                              resultArray[i] = result / inputArray[i];
                          }
          

          如果有帮助请采纳

          【讨论】:

            猜你喜欢
            • 2019-10-30
            • 1970-01-01
            • 2018-12-17
            • 1970-01-01
            • 2017-09-20
            • 2021-12-28
            • 1970-01-01
            • 2014-11-04
            • 1970-01-01
            相关资源
            最近更新 更多