如何在C#中为最接近的一组数字得到最接近的数字? [英] How do I get the closest set of numbers to a given set of numbers in C#?
问题描述
我有一组数字:
ABCDEFG
10 3 6 8 22 10 34
上面这组数字都在一个名为'Range'的类中:
class
{
int A;
int B;
int C;
int D;
int E;
int F;
int G;
}
我还有一个类型< range>的列表其中包含更多'Range'类型的对象。
例如:
I have a set of numbers:
A B C D E F G
10 3 6 8 22 10 34
The above set of numbers are in a class named, 'Range':
class
{
int A;
int B;
int C;
int D;
int E;
int F;
int G;
}
I also have a list of type <range> which contains more 'Range' typed objects.
Example:
List<Range> objects = new List<Range>();
假设我们在此列表中添加了2个范围:
15,1,2,10,40,1,33
0,0,100,0,2,1,3,3
现在我需要找到与我首先提到的范围最接近的值的范围,即:
ABCDEFG
10 3 6 8 22 10 34
我需要通过比较每封信来找到距离上述范围最近的范围值到所需范围的相应值。在这种情况下,15,1,2,10,40,1,33是最佳选择,因为它的大多数值都比第二个选项更接近所需范围的值。
我该怎样做这些人?
And let's say we have 2 ranges added in this list:
15, 1, 2, 10, 40, 1, 33
0, 0, 100, 0, 2, 1, 34
Now I need to find the ranges with the closest values to the Range I first mentioned, which is:
A B C D E F G
10 3 6 8 22 10 34
I need to find the range that has the closest range to the range mentioned above by comparing each Letter value to the required range's respective value. In this case, 15, 1, 2, 10, 40, 1, 33 is the best option since most of its values are closer to the values of the required range than the second option.
How can I do this folks?
推荐答案
这是一个建议。从目标
范围
中的相应字段中减去测试范围
中的每个字段。取每个结果的绝对值并求它们。总赢额最小的范围
。可以使用Math.Absolute(x)
找到x的绝对值。更复杂的方法是将绝对值表示为该字段的最大可能值的百分比。
Here is a suggestion. Subtract each field in the test
Range
from the corresponding field in the targetRange
. Take the absolute value of each result and sum them. TheRange
with the smallest total wins. The absolute value of x can be found usingMath.Absolute(x)
. A more sophisticated method would be to express the absolute value as a percentage of the maximum possible value of that field.
这篇关于如何在C#中为最接近的一组数字得到最接近的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!