将值与数组进行比较并获得最接近它的值



我是C#的新手,我正在尝试学习这种语言。

你们能给我一个提示,如何比较数组和从中挑选最低的值吗?

喜欢:

Double[] w = { 1000, 2000, 3000, 4000, 5000 };
double min = double.MaxValue;
double max = double.MinValue;
foreach (double value in w)
{
    if (value < min)
        min = value;
    if (value > max)
        max = value;
}
Console.WriteLine(" min:", min); 

给了我最低的w值,现在怎么比较?

如果我有:

int p = 1001 + 2000;  // 3001

我现在如何与数组列表进行比较,并发现 (3000) 值是最接近我的"搜索值"的值?

你可以用一些简单的数学来做到这一点,并且有不同的方法。

林克

Double searchValue = ...;
Double nearest = w.Select(p => new { Value = p, Difference = Math.Abs(p - searchValue) })
                  .OrderBy(p => p.Difference)
                  .First().Value;

手动地

Double[] w = { 1000, 2000, 3000, 4000, 5000 };
Double searchValue = 3001;
Double currentNearest = w[0];
Double currentDifference = Math.Abs(currentNearest - searchValue);
for (int i = 1; i < w.Length; i++)
{
    Double diff = Math.Abs(w[i] - searchValue);
    if (diff < currentDifference)
    {
        currentDifference = diff;
        currentNearest = w[i];
    }
}
Double[] w = { 1000, 2000, 3000, 4000, 5000 };
var minimumValueFromArray = w.Min();

生产

1000,正如预期的那样,因为我们执行枚举。

Enumerable.Max也是如此,要计算出最大值:

Double[] w = { 1000, 2000, 3000, 4000, 5000 };
var maximumValueFromArray = w.Max();

考虑到您正在与double.MinValuedouble.MaxValue进行比较,我假设您只想从数组中选择最小和最大的值。

如果这不是您要搜索的内容,请澄清。

基于您的代码,您可以通过非常简单的方式实现此目的

    Double[] w = { 1000, 2000, 3000, 4000, 5000 }; // it has to be sorted
    double search = 3001;
    double lowerClosest = 0;
    double upperClosest = 0;

        for (int i = 1; i < w.Length; i++)
        {
            if (w[i] > search)
            {
                upperClosest = w[i];
                break; // interrupts your foreach
            }
        }
        for (int i = w.Length-1; i >=0; i--)
        {
            if (w[i] <= search)
            {
                lowerClosest = w[i];
                break; // interrupts your foreach
            }
        }
    Console.WriteLine(" lowerClosest:{0}", lowerClosest);
    Console.WriteLine(" upperClosest:{0}", upperClosest);
    if (upperClosest - search > search - lowerClosest)
        Console.WriteLine(" Closest:{0}", lowerClosest);
    else
        Console.WriteLine(" Closest:{0}", upperClosest);
    Console.ReadLine();

根据您的搜索值的位置,这将小于 O(n)

                Performance wise custom code will be more use full. 
                List<int> results;
                int targetNumber = 0;
                int nearestValue=0;
                if (results.Any(ab => ab == targetNumber ))
                {
                    nearestValue= results.FirstOrDefault<int>(i => i == targetNumber );
                }
                else
                {
                    int greaterThanTarget = 0;
                    int lessThanTarget = 0;
                    if (results.Any(ab => ab > targetNumber ))
                    {
                        greaterThanTarget = results.Where<int>(i => i > targetNumber ).Min();
                    }
                    if (results.Any(ab => ab < targetNumber ))
                    {
                        lessThanTarget = results.Where<int>(i => i < targetNumber ).Max();
                    }
                    if (lessThanTarget == 0 )
                    {
                        nearestValue= greaterThanTarget;
                    }
                    else if (greaterThanTarget == 0)
                    {
                        nearestValue= lessThanTarget;
                    }
                    else if (targetNumber - lessThanTarget < greaterThanTarget - targetNumber )
                    {
                        nearestValue= lessThanTarget;
                    }
                    else
                    {
                            nearestValue= greaterThanTarget;
                    }
                }

相关内容

  • 没有找到相关文章

最新更新