Хочу вывести на консоль людей, между которыми минимальнок и максимальное растояние с помощью LINQ
internal class GeoCordinate
{
public double Latitude { get; set; }
public double Longitude { get; set; }
public GeoCordinate(double latitude, double longitude)
{
Latitude = latitude;
Longitude = longitude;
}
}
internal class Person
{
public string Name;
public string SecondName;
public GeoCordinate Location;
public Person(string name, string secondName, GeoCordinate location)
{
Name = name;
SecondName = secondName;
Location = location;
}
}
internal class Program
{
static void Main(string[] args)
{
List<Person> list = new List<Person>
{
new Person("Vlad", "Pob", new GeoCordinate(-54.32, 84.63)),
new Person("Oleg", "Berd", new GeoCordinate(3.37, -115.48)),
new Person("Kiril", "Old", new GeoCordinate(-23.0, 45.89)),
new Person("Vasya", "Melt", new GeoCordinate(88.7, 2.12)),
new Person("Stepan", "Valid", new GeoCordinate(34.86, 154.35)),
};
// find out who is located farthest north / south / west / east using latitude/ longitude data
//find max and min distance between 2 persons
var northiest = list.MaxBy(p => p.Location.Latitude);
var southiest = list.MinBy(p => p.Location.Latitude);
var westiest = list.MaxBy(p => p.Location.Longitude);
var eastier = list.MinBy(p => p.Location.Longitude);
Console.WriteLine($"Northies:{northiest.Name + " " + northiest.SecondName}" +
$"\nSouthiest: {southiest.Name + " " + southiest.SecondName}" +
$"\nEastier: {eastier.Name + " " + eastier.SecondName}" +
$"\nWestiest: {westiest.Name + " " + westiest.SecondName} ");
}
}
Ответы (1 шт):
Автор решения: Виктор
→ Ссылка
класс
internal class GeoCordinate
{
public double Latitude { get; set; }
public double Longitude { get; set; }
public GeoCordinate(double latitude, double longitude)
{
Latitude = latitude;
Longitude = longitude;
}
public double Distance(GeoCordinate a)
{
return Math.Sqrt(Math.Pow(Latitude - a.Latitude, 2) +
Math.Pow(Longitude - a.Longitude, 2));
}
}
main:
List<Person> list = new List<Person>
{
new Person("Vlad", "Pob", new GeoCordinate(-54.32, 84.63)),
new Person("Oleg", "Berd", new GeoCordinate(3.37, -115.48)),
...
};
Dictionary<string, double> dict = new Dictionary<string, double>();
// Ищем расстояние всех со всеми
for(int x=0; x<list.Count; x++)
{
for(int y=x+1; y < list.Count; y++)
{
var name = $"{list[x].Name}_{list[x].SecondName} - {list[y].Name}_{list[y].SecondName}";
dict.Add(name, list[x].Location.Distance(list[y].Location));
}
}
var maxDist = dict.Aggregate((x, y) => x.Value > y.Value ? x : y);
var minDist = dict.Aggregate((x, y) => x.Value > y.Value ? y : x);
Console.WriteLine($"Max: {maxDist}\nMin: {minDist}");
Вывод:
Max: [Oleg_Berd - Stepan_Valid, 271.66127622464]
Min: [Vlad_Pob - Kiril_Old, 49.8169649818212]
Вариант с LINQ
var pairs = from i in Enumerable.Range(0, list.Count - 1)
let x = list[i]
from y in list.Skip(i + 1)
select Tuple.Create(
$"{x.Name}_{x.SecondName} - {y.Name}_{y.SecondName}",
x.Location.Distance(y.Location));
var maxD= pairs.Aggregate((x, y) => x.Item2 > y.Item2 ? x : y);
var minD = pairs.Aggregate((x, y) => x.Item2 > y.Item2 ? y : x);
Console.WriteLine($"Max: {maxD}\nMin: {minD}");
Вывод:
Max: (Oleg_Berd - Stepan_Valid, 271.66127622464)
Min: (Vlad_Pob - Kiril_Old, 49.8169649818212)
Что понятнее - решать Вам