C# 在浮动列表中查找最近的数字
嗨,我有一张花车清单C# 在浮动列表中查找最近的数字,c#,arrays,find,C#,Arrays,Find,嗨,我有一张花车清单 List<float> distance = new List<float>(); distance.Add(66.2F); distance.Add(69.9F); distance.Add(70.2F); distance.Add(70.3F); distance.Add(70.6F); distance.Add(71.4F);
List<float> distance = new List<float>();
distance.Add(66.2F);
distance.Add(69.9F);
distance.Add(70.2F);
distance.Add(70.3F);
distance.Add(70.6F);
distance.Add(71.4F);
distance.Add(71.6F);
列表距离=新列表();
距离。添加(66.2F);
距离。加上(69.9F);
距离。添加(70.2F);
距离。加上(70.3F);
距离。加上(70.6F);
距离。加上(71.4F);
距离。加上(71.6F);
我有一个扩展方法,可以从这个数组中得到最近的数
public static class extenstions
{
public static float ClosestTo(this IEnumerable<float> collection, float target)
{
var closest = float.MaxValue;
var minDifference = float.MaxValue;
foreach (var element in collection)
{
var difference = Math.Abs((long)element - target);
if (minDifference > difference)
{
minDifference = (int)difference;
closest = element;
}
}
return closest;
}
}
公共静态类扩展
{
公共静态float ClosestTo(此IEnumerable集合,float目标)
{
var最近=float.MaxValue;
var minDifference=float.MaxValue;
foreach(集合中的var元素)
{
var差异=数学Abs((长)元素-目标);
如果(MindDifference>difference)
{
心智差异=(智力)差异;
最近的=元素;
}
}
返回最近的位置;
}
}
浮动最近=距离。最接近(70F);
现在,如果我想找到最接近的数字70,它将返回70.2。我想修改这个函数,以返回不太接近的69.9,而不是更高的值70.2。
您能帮忙吗?使用Linq:
public static float ClosestTo(this IEnumerable<float> collection, float target)
{
return collection.OrderBy(x => Math.Abs(target - x)).First();
}
它比使用OrderBy
要好,因为它在O(n)
中运行,而不是在O(n log n)
中运行
编辑:如果两个数字与目标值相等,并且您想要两个数字中的较大者,则可以执行以下操作:
public static float ClosestTo(this IEnumerable<float> collection, float target)
{
return collection
.OrderBy(x => Math.Abs(target - x))
.ThenByDescending(x => x)
.First();
}
publicstaticfloatclosestto(此IEnumerable集合,float目标)
{
回收
.OrderBy(x=>Math.Abs(target-x))
.然后按降序(x=>x)
.First();
}
可能是当你将值投射到长距离时,你在昏迷70,2->70和69后丢失了数字,9->69和70最接近70而不是69
如果你想从左或右取最近的距离,请使用以下选项:
public static float ClosestTo(this IEnumerable<float> collection, float target)
{
float a = collection.Where(x => x >= target).OrderBy(x => x - target).First();
float b = collection.Where(x => x < target).OrderBy(x => Math.Abs(target - x)).First();
return a.Equals(b) ? a : Math.Min(a, b);
}
publicstaticfloatclosestto(此IEnumerable集合,float目标)
{
float a=collection.Where(x=>x>=target).OrderBy(x=>x-target).First();
float b=collection.Where(x=>xMath.Abs(target-x)).First();
返回a.Equals(b)?a:Math.Min(a,b);
}
其中,
a
是右的,b
是左的(如果你想左改返回a.Equals(b)?a:Math.Min(a,b);
到返回a.Equals(b)?b:Math.Min(a,b);
不要将元素变量强制转换为long。为什么要将元素强制转换为long
?如果到处都使用浮点数,那就没有意义了。只需将该强制转换移除为long
work@SriramSakthivel,不,您还需要删除int的强制转换…嗨,如果我有这个值怎么办69.9 70.1,我想返回最近的值,它返回69.9,我想得到70.1??,所以如果调用closestto(70)它返回69.9我们能把它改为返回70.1吗?@IkramKhan,基于什么标准,你会选择70.1而不是69.9?实际上是否有可能传递函数,或者我们希望目标得到更少的cloasest数字,或者目标得到更大的cloasest数字?看看我的评论,也许这会有帮助我编辑它,这很有帮助谢谢我来美国p根据我的要求使用此函数leftdistance=target-b;rightdistance=a-target;if(leftdistance==rightdistance){return a;}elseif leftdistance
public static float ClosestTo(this IEnumerable<float> collection, float target)
{
float a = collection.Where(x => x >= target).OrderBy(x => x - target).First();
float b = collection.Where(x => x < target).OrderBy(x => Math.Abs(target - x)).First();
return a.Equals(b) ? a : Math.Min(a, b);
}