C# 列出整数,为每个索引查找除该索引外的每个整数的乘积

C# 列出整数,为每个索引查找除该索引外的每个整数的乘积,c#,linq,C#,Linq,您有一个整数列表,对于每个索引,您希望找到除该索引处的整数外的每个整数的乘积 我无法理解我的代码有什么问题: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Ma

您有一个整数列表,对于每个索引,您希望找到除该索引处的整数外的每个整数的乘积

我无法理解我的代码有什么问题:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int outcome = 1;
            int removed;
            List<int> list = new List<int>(new int[] { 1, 2, 6, 4, 6, 4, 9, 
        8, 3, 1 });
            for (int j = 0; j < 9; j++)
            {
                removed=list[j] ;
                list.RemoveAt(j);
                 for (int i = 0; i < 9; i++)
                 {
                     outcome *= list[i];
                 }
                 list.Add(removed);
                 Console.WriteLine(outcome);
            }
              Console.ReadKey();
        }
     }

    }

我认为问题在于,您仅在程序开始时将结果定义为1——据我所知,您希望在int j=0的第一个for循环中启动它;j<9;j++

找到列表中每个整数的乘积,然后除以索引中的整数不是更容易吗

for (int j = 0; j < 9; j++) 
{
    outcome = 1;

    for (int i = 0; i < 9; i++) 
        outcome *= list[i];

    outcome = outcome / list[j];
    Console.WriteLine(outcome);
}

问题是,每次迭代后都不会重置结果,所以结果会越来越大。您的程序的一个简单修复程序是:

        int removed;
        List<int> list = new List<int>(new int[] { 1, 2, 6, 4, 6, 4, 9, 
    8, 3, 1 });
        for (int j = 0; j < 9; j++)
        {
            removed=list[j] ;
            list.RemoveAt(j);
            int outcome = 1;

             for (int i = 0; i < 9; i++)
             {
                 outcome *= list[i];
             }
             list.Add(removed);
             Console.WriteLine(outcome);
        }
但是,通过实现这个算法,您可以显著提高效率。我不为您编写cod,但您可以自己轻松编写一个

计算所有元素的乘积,称之为乘积 循环遍历每个元素,在每个索引j处,打印 产品/清单[j] 必须为int i=0进行编辑;i<9;i++to表示int i=0;i
static void Main(string[] args)
{
    int outcome = 1;
    int removed;
    List<int> list = new List<int>(new int[] { 1, 2, 6, 4, 6, 4, 9, 
8, 3, 1 });
    for (int j = 0; j < 9; j++)
    {
        removed = list[j];
        list.RemoveAt(j);
        for (int i = 0; i < list.Count; i++)
        {
            outcome *= list[i];
        }
        list.Add(removed);
        Console.WriteLine(outcome);
    }
    Console.ReadKey();
}

您的代码有几个问题:

您更改了正在循环的列表 不重置结果变量 您不需要迭代整个列表,其中有10个元素,但只需迭代9个元素 一个有效的例子可以是:

for (int j = 0; j < list.Count; j++)
{
    int output = list.Where((val, idx) => idx != j).Aggregate((a, x) => a * x);
    Console.WriteLine(output);
}
Console.ReadKey();

您可以删除索引中的项,并将其添加到结束列表中。 不修改列表,只跳过需要的项目。例如:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestList
{
    class Program
    {
        static void Main(string[] args)
        {
            double outcome = 1;
            List<double> list = new List<double>(new double[] { 1, 2, 6, 4, 6, 4, 9, 8, 3, 1 });
            //List<int> list = new List<int>(new int[] { 1, 2, 3 });
            for (int i = 0; i < list.Count; i++)
            {
                for (int j = 0; j < list.Count; j++)
                {
                    if (i == j)
                        continue;
                    outcome *= list[j];
                }
            }
            Console.WriteLine(outcome);
            Console.WriteLine("Press any key to continue . . .");
            Console.ReadKey();
        }
    }
}
请注意,使用double,因为值太高

您有两个问题:

在迭代列表中的项目时,您正在更改这些项目的顺序。 在外循环中,您没有将结果重置为1,因此它会越来越大。 你可以考虑另一种方法:

计算列表中所有数字的乘积。 对于列表中的每个数字,将总乘积除以该数字,得到不包括该数字的乘积。 在一般情况下,您可能应该使用long来保存产品,以帮助避免溢出,尽管足够长的列表和足够大的数字仍然可能溢出long

您可以使用Linq的聚合计算产品,如下所示:

List<int> list = new List<int>(new[] {1, 2, 6, 4, 6, 4, 9, 8, 3, 1});
long prod = list.Aggregate(1L, (p, v) => p * v); // 1L means it will use long.
foreach (var i in list)
    Console.WriteLine(prod / i);

请注意,如果列表中的任何元素为零,则这将不起作用。在这种情况下,每一个结果都应该是零。

那怎么了?你改变列表移除,然后在其上循环…对于给定的输入列表,你期望得到什么输出?问题集中提到了这一点,在你的解决方案中不要使用除法。thanx先生相对于所有其他参与者,你的答案是最好的,thans先生:*