Advertisement
braveheart1989

dsa

Jun 8th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _05.ClosestTwoPoints
  8. {
  9.     class Point
  10.     {
  11.         public int X { get; set; }
  12.         public int Y { get; set; }
  13.  
  14.         public override string ToString()
  15.         {
  16.             return "(" + X + ", " + Y + ")";
  17.         }
  18.     }
  19.     class ClosesTwoPoints
  20.     {
  21.         public Point point1 { get; set; }
  22.         public Point point2 { get; set; }
  23.         public double distance { get; set; }
  24.     }
  25.  
  26.     class ClosestTwoPoints
  27.     {
  28.         static void Main(string[] args)
  29.         {
  30.             Point[] points = ReadPoints();
  31.             var closesTwoPoints = FindClosestTwoPoint(points);
  32.             Console.WriteLine("{0:F3}", closesTwoPoints.distance);
  33.             Console.WriteLine(closesTwoPoints.point1);
  34.             Console.WriteLine(closesTwoPoints.point2);
  35.         }
  36.  
  37.         static ClosesTwoPoints FindClosestTwoPoint(Point[] points)
  38.         {
  39.             double minDist = double.MaxValue;
  40.             ClosesTwoPoints result = null;
  41.             for (int i = 0; i < points.Length; i++)
  42.             {
  43.                 for (int j = i+1; j < points.Length; j++)
  44.                 {
  45.                     double calcDistance = CalcDistance(points[i], points[j]);
  46.                     if (calcDistance < minDist)
  47.                     {
  48.                         minDist = calcDistance;
  49.                         result = new ClosesTwoPoints()
  50.                         {
  51.                             point1 = points[i],
  52.                             point2 = points[j],
  53.                             distance = minDist
  54.                            
  55.                         };
  56.                     }
  57.                 }
  58.             }
  59.             return result;
  60.         }
  61.  
  62.         static Point[] ReadPoints()
  63.         {
  64.             int n = int.Parse(Console.ReadLine());
  65.             Point[] points = new Point[n];
  66.             for (int i = 0; i < n; i++)
  67.             {
  68.                 points[i] = ReadPoint();
  69.             }
  70.             return points;
  71.         }
  72.  
  73.         static Point ReadPoint()
  74.         {
  75.             int[] nums = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  76.             Point point = new Point() { X = nums[0], Y = nums[1] };
  77.             return point;
  78.         }
  79.  
  80.         static double CalcDistance(Point p1, Point p2)
  81.         {
  82.             double deltaX = p1.X - p2.X;
  83.             double deltaY = p1.Y - p2.Y;
  84.             double dist = Math.Sqrt(deltaX * deltaX + deltaY * deltaY);
  85.             return dist;
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement