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

本文关键字:十进制数 数组 复杂 计算 | 更新日期: 2023-09-27 17:49:42

我有一个列表框,用户可以在其中输入十进制数字。假设他们将输入5个数字:

1.1
1.2
1.3
1.4 
1.5

我需要得到这5个数字的所有变化的总和。例如1.1 and 1.21.1 1.2 1.31.1 1.2 1.3 1.41.2 1.4 1.51.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);
}   

我需要找到一种方法来计算我想要的方式

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

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

如果总是有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;
    }
}

这里没有任何代码来避免两次添加相同的数字(我将让您尝试完成它。提示:您可以简单地在增量计数器部分之后这样做,在while循环中包含"增量计数器"部分和新的"检查计数器"部分,当计数器是唯一时,在while循环外中断…

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

只是一个提纲,因为我在我的手机上:

从输入列表和包含0的输出列表开始。

对于输入中的每个数字,通过将当前输入的数字添加到当前输出列表中的每个数字,创建一个新的双精度序列;然后将此列表连接到输出列表的末尾。

可选地,删除每个输入数字的零和第一个实例,以及任何重复的:

。对于您的示例输入最多1.4:

0
0 1.1
0 1.1 1.2 2.3
0 1.1 1.2 2.3 1.3 2.4 2.5 3.6
0 1.1 1.2 2.3 1.3 2.4 2.5 3.6 1.4 2.5 2.6 3.7 2.7 3.8 3.9 5.0
         1.2 2.3      2.4 2.5 3.6         2.6 3.7 2.7 3.8 3.9 5.0                    

虽然我不是很精通c#,但我肯定有一种更简单的方法来做你想做的事情;当然,除非我遗漏了什么。

为什么不为List或Array中的每个元素创建一个for循环,然后告诉它跳过自己呢?例子:

Double[] array = new Double[3];
array[0] = 1,1;
array[1] = 1,2;
array[2] = 1,3;
Double sum = 0;
for ( int i = 0; i < array.Length ; i++ )
{
    for ( int x = 0 ; x < array.Length ; x++ ) {
        if ( array[i] != array[x] )
        {
            sum = array[x] + array[x+1] // or [x-1] depending on the value of x, you should be able to work this out.   
        }
    }
}
通过检查这个例子,你应该能够理解我的意思。当然,这是一个非常基本的原型,你要做的是扩展它来根据x的值向后检查,并有多个"sum"变量来存储你的总和——这取决于你想要的结果。

-我希望这有助于,圣诞快乐。

拿你的listBox,在每个数字的前面,要么放一个0,表明它将参与你的总和,要么放一个1,表明它参与你的总和。有了1.1, 1.2, 1.3, 1.4, 1.5的示例列表以及1.1, 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的总和,这将给你(为了清晰起见,我只写1 s,空格表示0):

         |     |     | 1.1 |     |
         |     | 1.1 | 1.2 | 1.2 | 1.1
         |     | 1.2 | 1.3 | 1.4 | 1.3
     1.1 | 1.2 | 1.3 | 1.4 | 1.5 | 1.5
---+-----+-----+-----+-----+-----+-----
1.1|  1           1     1           1
1.2|        1     1     1     1
1.3|              1     1           1
1.4|                    1     1
1.5|                          1     1

现在可以看到,列出这些数字的所有组合现在类似于从0到31的计数(二进制11111,2⁵ - 1)。如果您对空序列不感兴趣,则从1开始计数。

下面是将此计数转换为您想要的listNumber的示例代码。请原谅语法,因为我不懂c#。这也意味着这是未经测试的代码。

Double[] array = new Double[listBox1.Items.Count];
for (int i = 0; i < listBox1.Items.count; i++)
    array[k] = Convert.ToDouble(listBox1.Items[i]);
int count = 2 ^ array.Items.Count;
List<Double>[] listNumber = new List<Double>[count];
for (int i = 0; i < listNumber.Items.Count; i++) {
    listNumber[i] = new List<Double>();
    for (j = 0; j < array.Items.Count)
        if (i & (1 << j) != 0)
            listNumber[i].Add(array[j]);
}