C#列表中的最小差异 [英] C# Minimum difference in a list

查看:79
本文介绍了C#列表中的最小差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在xy点数据列表中找到最小的差异. (可以包含重复的x值和y值)

I want to find minimum difference in a list of xy point data. (can contain duplicate x values and y values)

Example:
List<Point> differenceList = new List<Point>() { (10,0), (10,20), (20,30), (12,61) };

预期结果

X difference = 2 
Y difference = 20 

是否可以使用linq/lambda表达式优雅地做到这一点? 谢谢!

is it possible to do this elegantly may be using linq/lambda expressions? Thanks!

推荐答案

class Point
{
    public int X { get; set; }
    public int Y { get; set; }
    public Point(int x, int y)
    {
        X = x;
        Y = y;
    }
}

class Program
{
    static void Main(string[] args)
    {
        List<Point> differenceList =
            new List<Point>()
            {
                new Point(40, 60), 
                new Point(10, 20),
                new Point(20, 30), 
                new Point(12, 61) };

        var q = from p1 in differenceList
                from p2 in differenceList
                let distance = Math.Abs(p1.X - p2.X)
                where !object.ReferenceEquals(p1, p2)
                select new { Point1 = p1, Point2 = p2, Distance = distance };

        var minimum = q.OrderBy(r => r.Distance).First();
        Console.WriteLine(
            "X difference = " +
            minimum.Distance +
            " (which is " +
            Math.Max(minimum.Point1.X, minimum.Point2.X) +
            " - " +
            Math.Min(minimum.Point1.X, minimum.Point2.X) + ")");

        Console.ReadLine();
    }
}

您可以编写查询并在单个语句中选择最小值,如下所示:

You could write the query and selecting the minimum in a single statement like this:

var minimum = (from p1 in differenceList
                from p2 in differenceList
                let distance = Math.Abs(p1.X - p2.X)
                where !object.ReferenceEquals(p1, p2)
                orderby distance
                select new { Point1 = p1, Point2 = p2, Distance = distance }).First();

对y的查询几乎相同并且很简单

The query for y is nearly the same and trivial

编辑

包含重复项且正在使用System.Windows.Point的集合的解决方案:

Solution for a collection that contains duplicates and is using System.Windows.Point:

class Program
{
    static void Main(string[] args)
    {
        List<Point> differenceList =
            new List<Point>()
        {
            new Point(40, 60), 
            new Point(10, 20),
            new Point(20, 30), 
            new Point(12, 61),
            new Point(10, 20)};

        var q = from p1 in differenceList
                from p2 in differenceList
                let distance = Math.Abs(p1.X - p2.X)
                where !p1.Equals(p2)
                select new { Point1 = p1, Point2 = p2, Distance = distance };

        var minimum = q.OrderBy(r => r.Distance).First();
        Console.WriteLine(
            "X difference = " +
            minimum.Distance +
            " (which is " +
            Math.Max(minimum.Point1.X, minimum.Point2.X) +
            " - " +
            Math.Min(minimum.Point1.X, minimum.Point2.X) + ")");

        Console.ReadLine();
    }
}

这篇关于C#列表中的最小差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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