【问题标题】:Calculate (complicated) array of decimal numbers in C#在 C# 中计算(复杂的)十进制数数组
【发布时间】:2012-12-11 16:13:08
【问题描述】:

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

1.1
1.2
1.3
1.4 
1.5

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

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

【问题讨论】:

  • 这是家庭作业吗?

标签: c# arrays list math arraylist


【解决方案1】:

虽然我对 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”变量来存储总和 - 取决于您的结果类型正在寻找。

-我希望这会有所帮助,圣诞快乐。

【讨论】:

  • 这并没有给出所有的可能性。
【解决方案2】:

我在手机上只是一个大纲:

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

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

(可选)删除每个输入数字的零和第一个实例,以及所有重复项:

例如对于您的示例输入高达 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                    

【讨论】:

    【解决方案3】:

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

    如果总是有 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 循环之外中断...

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

    【讨论】:

      【解决方案4】:

      填写您的listBox,并在每个数字前添加0 表示它不会参与您的总和,或者输入1 表示它参与到您的总和中。使用1.11.21.31.41.5 的示例列表以及1.11.2 然后1.1 1.2 1.3 然后1.1 1.2 1.3 1.4 然后1.2 1.4 1.59876543333 的总和@这会给你(为了清楚起见,我只写1s,空格表示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]);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-03-01
        • 2019-04-29
        • 1970-01-01
        • 2012-09-12
        • 1970-01-01
        • 2015-03-10
        • 1970-01-01
        相关资源
        最近更新 更多