日期:2014-05-18 浏览次数:21014 次
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Point> data = new List<Point>()
            {
                new Point() { X = 1, Y = 4 },
                new Point() { X = 3, Y = 8 },
                new Point() { X = 9, Y = 20 },
                new Point() { X = 17, Y = 25 }
            };
            data.ForEach(x =>
            {
                if (data.Where(y => y != x).Any(y => y.X <= x.Y && y.Y >= x.X))
                {
                    var desc = data.Where(y => y != x).First(y => y.X <= x.Y && y.Y >= x.X);
                    x.X = desc.X > x.X ? x.X : desc.X;
                    x.Y = desc.Y > x.Y ? desc.Y : x.Y;
                }
            });
            data = data.GroupBy(x => new { x.X, x.Y }).Select(x => new Point() { X = x.Key.X, Y = x.Key.Y }).ToList();
            foreach (var item in data)
            {
                Console.WriteLine(item);
            }
        }
    }
    class Point
    {
        public int X { get; set; }
        public int Y { get; set; }
        public override string ToString()
        {
            return string.Format("X = {0}, Y = {1}.", X, Y);
        }
    }
}