Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _05.ClosestTwoPoints
- {
- class Point
- {
- public int X { get; set; }
- public int Y { get; set; }
- public override string ToString()
- {
- return "(" + X + ", " + Y + ")";
- }
- }
- class ClosesTwoPoints
- {
- public Point point1 { get; set; }
- public Point point2 { get; set; }
- public double distance { get; set; }
- }
- class ClosestTwoPoints
- {
- static void Main(string[] args)
- {
- Point[] points = ReadPoints();
- var closesTwoPoints = FindClosestTwoPoint(points);
- Console.WriteLine("{0:F3}", closesTwoPoints.distance);
- Console.WriteLine(closesTwoPoints.point1);
- Console.WriteLine(closesTwoPoints.point2);
- }
- static ClosesTwoPoints FindClosestTwoPoint(Point[] points)
- {
- double minDist = double.MaxValue;
- ClosesTwoPoints result = null;
- for (int i = 0; i < points.Length; i++)
- {
- for (int j = i+1; j < points.Length; j++)
- {
- double calcDistance = CalcDistance(points[i], points[j]);
- if (calcDistance < minDist)
- {
- minDist = calcDistance;
- result = new ClosesTwoPoints()
- {
- point1 = points[i],
- point2 = points[j],
- distance = minDist
- };
- }
- }
- }
- return result;
- }
- static Point[] ReadPoints()
- {
- int n = int.Parse(Console.ReadLine());
- Point[] points = new Point[n];
- for (int i = 0; i < n; i++)
- {
- points[i] = ReadPoint();
- }
- return points;
- }
- static Point ReadPoint()
- {
- int[] nums = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- Point point = new Point() { X = nums[0], Y = nums[1] };
- return point;
- }
- static double CalcDistance(Point p1, Point p2)
- {
- double deltaX = p1.X - p2.X;
- double deltaY = p1.Y - p2.Y;
- double dist = Math.Sqrt(deltaX * deltaX + deltaY * deltaY);
- return dist;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement