Advertisement
tanya_zheleva

Untitled

Feb 11th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ExamPreparation
  6. {
  7.     public sealed class Startu
  8.     {
  9.         public static void Main()
  10.         {
  11.             int n = int.Parse(Console.ReadLine());
  12.             List<Point> points = new List<Point>();
  13.  
  14.             for (int i = 0; i < n; i++)
  15.             {
  16.                 double[] pointTokens = Console.ReadLine()
  17.                     .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  18.                     .Select(double.Parse)
  19.                     .ToArray();
  20.  
  21.                 Point point = new Point(pointTokens[0], pointTokens[1]);
  22.                 points.Add(point);
  23.             }
  24.  
  25.             double minDistance = double.MaxValue;
  26.             Point minFirstPoint = null;
  27.             Point minSecondPoint = null;
  28.  
  29.             for (int i = 0; i < points.Count; i++)
  30.             {
  31.                 Point firstPoint = points[i];
  32.  
  33.                 for (int j = i + 1; j < points.Count; j++)
  34.                 {
  35.                     Point secondPoint = points[j];
  36.                     double distance = firstPoint.CalculateDistance(secondPoint);
  37.  
  38.                     if (distance < minDistance)
  39.                     {
  40.                         minDistance = distance;
  41.                         minFirstPoint = firstPoint;
  42.                         minSecondPoint = secondPoint;
  43.                     }
  44.                 }
  45.             }
  46.  
  47.             Console.WriteLine($"{minDistance:F3}");
  48.             Console.WriteLine(minFirstPoint);
  49.             Console.WriteLine(minSecondPoint);
  50.         }
  51.     }
  52.  
  53.     public sealed class Point
  54.     {
  55.         public Point(double x, double y)
  56.         {
  57.             this.X = x;
  58.             this.Y = y;
  59.         }
  60.  
  61.         public double X { get; private set; }
  62.  
  63.         public double Y { get; private set; }
  64.  
  65.         public double CalculateDistance(Point point)
  66.         {
  67.             double a = Math.Abs(this.X - point.X);
  68.             double b = Math.Abs(this.Y - point.Y);
  69.             double c = Math.Sqrt(a * a + b * b);
  70.  
  71.             return c;
  72.         }
  73.  
  74.         public override string ToString()
  75.         {
  76.             return $"({this.X}, {this.Y})";
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement