计算(复杂)C#中的十进制数组

Laz*_*ale 5 c# arrays math list arraylist

大家好,圣诞快乐为今天的人们庆祝.
我有问题,也许有人可以帮助我.
我有一个列表框,用户可以在其中输入十进制数字.
让我们说他们将输入5个数字:

1.1
1.2
1.3
1.4 
1.5
Run Code Online (Sandbox Code Playgroud)

我需要得到这5个数字中所有变化的总和.例如,1.1 and 1.2那么1.1 1.2 1.3之后的总和1.1 1.2 1.3 1.4,1.2 1.4 1.5然后1.1 1.3 1.5.
我开始做了一些事情,但是所有的变化都只是一次跳过一个数字:

List<Double[]> listNumber = new List<double[]>();            
Double[] array;            
for (int i = 0; i < listBox1.Items.Count; i++)
{
    array = new Double[listBox1.Items.Count];                
    for (int k = 0; k < listBox1.Items.Count; k++)
    {
        if (!k.Equals(i))
        {
            array[k] = (Convert.ToDouble(listBox1.Items[k]));                       
        }
    }
    listNumber.Add(array);
}   
Run Code Online (Sandbox Code Playgroud)

我需要找到一种方法如何计算我想要的方式,如果有人能给我sime想法它将是伟大的圣诞礼物:)在此先感谢,Laziale

Tod*_*odd 1

在您最初的尝试中,您的代码仅计算所有可能对的总和。根据你的描述,你还想找到三个数字的总和,等等。

如果总是有 5 个小数,那么您可以简单地使用 5 个 for 循环。然而,更通用的设计会更干净

double[] input = double[5]; //Pretend the user has entered these
int[] counters = int[input.Length]; //One for each "dimension"
List<double> sums = new List<double>();

for (int i = 0; i < counters.Length; i++)
   counters[i] = -1; //The -1 value allows the process to begin with sum of single digits, then pairs, etc..

while (true)
{
    double thisSum = 0;
    //Apply counters
    for (int i = 0; i < counters.Length; i++)
    {
        if (counters[i] == -1) continue; 

        thisSum += input[counters[i]];
    }

    //Increment counters
    counters[0]++; //Increment at base
    for (int i = 0; i < counters.Length; i++)
    {
        if (counters[i] >= counters.Length)
        {
            if (i == counters.Length - 1) //Check if this is the last dimension
               return sums; //Exhausted all possible combinations

            counters[i] = 0;
            counters[i+1]++;
        }
        else
           break;
    }
}
Run Code Online (Sandbox Code Playgroud)

这里没有任何代码来避免两次添加相同的数字(我将让您尝试完成该操作。提示:您可以在增量计数器部分之后简单地执行此操作,其中包含“增量计数器”部分和while 循环内的新“检查计数器”部分,当计数器唯一时中断 while 循环外部...

注意:我还没有测试过这段代码,但它会很接近,并且可能会存在一两个错误 - 如果您需要任何有关错误的帮助,请告诉我。