找到正确数字集的算法 [英] algorithm to find the correct set of numbers

查看:34
本文介绍了找到正确数字集的算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将使用c#解决方案的python

i will take either python of c# solution

我大约有200个号码:

i have about 200 numbers:

 19.16 
 98.48 
 20.65 
 122.08 
 26.16 
 125.83 
 473.33 
 125.92 
 3,981.21 
 16.81 
 100.00 
 43.58 
 54.19 
 19.83 
 3,850.97 
 20.83 
 20.83 
 86.81 
 37.71 
 36.33 
 6,619.42 
 264.53 
...
...

我知道在这组数字中,会有一个数字组合,这些数字加起来就等于某个数字,假设它是 2341.42

i know that in this set of numbers, there is a combination of numbers that will add up to a certain number let's say it is 2341.42

我如何找出哪种数字组合加起来呢?

how do i find out which combination of numbers will add up to that?

我正在帮助会计人员找出正确的数字

i am helping someone in accounting track down the correct numbers

推荐答案

[开始编辑]:

我误解了原始问题.我以为是说200多个数字列表中有4个数字的组合,这些数字加起来就等于其他数字.那不是被要求的,所以我的回答并没有多大帮助.

I misread the original question. I thought that it said that there is some combination of 4 numbers in the list of 200+ numbers that add up to some other number. That is not what was asked, so my answer does not really help much.

[结束编辑]

这很笨拙,但是如果您需要找到的四个数字加起来等于某个值(它可以找到四个以上的元组),那么它应该可以工作:

This is pretty clunky, but it should work if all you need is to find the 4 numbers that add up to a certain value (it could find more than 4 tuples):

只需将200个数字放入一个数组(或列表或某个IEnumerable结构)中,然后就可以使用我发布的代码了.如果您在纸上有数字,则必须手动将它们输入到数组中,如下所示.如果在软拷贝中包含它们,则可以剪切并粘贴它们,然后在它们周围添加数字[x] = xxx代码.或者,您可以将它们剪切并粘贴到文件中,然后从磁盘将其读取到阵列中.

Just get your 200 numbers into an array (or list or some IEnumerable structure) and then you can use the code that I posted. If you have the numbers on paper, you will have to enter them into the array manually as below. If you have them in softcopy, you can cut and paste them and then add the numbers[x] = xxx code around them. Or, you could cut and paste them into a file and then read the file from disk into an array.

  double [] numbers = new numbers[200];
  numbers[0] = 123;
  numbers[1] = 456; 

  //
  // and so on.
  //

  var n0 = numbers;
  var n1 = numbers.Skip(1);
  var n2 = numbers.Skip(2);
  var n3 = numbers.Skip(3);

  var x = from a in n0
          from b in n1
          from c in n2
          from d in n3
          where a + b + c + d == 2341.42
          select new { a1 = a, b1 = b, c1 = c, d1 = d };

  foreach (var aa in x)
  {
    Console.WriteLine("{0}, {1}, {2}, {3}", aa.a1, aa.b1, aa.c1, aa.d1 );
  }

这篇关于找到正确数字集的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆