viraco4a

Untitled

Dec 29th, 2017
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 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 task_17
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. int n = int.Parse(Console.ReadLine());
  14. Point[] AllPoints = GetThePoints(n);
  15. Point[] ClosestPoints = FindClosestPoints(AllPoints);
  16. double minDistance = ClosestPoints[0].CalcDistance(ClosestPoints[1]);
  17.  
  18. Console.WriteLine("{0:f3}", minDistance);
  19. Console.WriteLine($"({ClosestPoints[0].X}, {ClosestPoints[0].Y})");
  20. Console.WriteLine($"({ClosestPoints[1].X}, {ClosestPoints[1].Y})");
  21. }
  22.  
  23. static Point[] GetThePoints(int numberOfPoints)
  24. {
  25. Point[] AllPoints = new Point[numberOfPoints];
  26. for (int i = 0; i < numberOfPoints; i++)
  27. {
  28. double[] splitted = Console.ReadLine().Split(' ').Select(s => double.Parse(s)).ToArray();
  29. Point currentPoint = new Point(splitted[0], splitted[1]);
  30. AllPoints[i] = currentPoint;
  31. }
  32.  
  33. return AllPoints;
  34. }
  35.  
  36. static Point[] FindClosestPoints(Point[] points)
  37. {
  38. Point[] ClosestPoints = new Point[2];
  39. ClosestPoints[0] = points[0];
  40. ClosestPoints[1] = points[1];
  41. double minDistance = points[0].CalcDistance(points[1]);
  42. for (int i = 0; i < points.Length; i++)
  43. {
  44. for (int j = i + 1; j < points.Length; j++)
  45. {
  46. if (points[i].CalcDistance(points[j]) < minDistance)
  47. {
  48. minDistance = points[i].CalcDistance(points[j]);
  49. ClosestPoints[0] = points[i];
  50. ClosestPoints[1] = points[j];
  51. }
  52. }
  53.  
  54. }
  55. return ClosestPoints;
  56. }
  57. }
  58.  
  59. class Point
  60. {
  61. public Point(double x, double y)
  62. {
  63. this.X = x;
  64. this.Y = y;
  65. }
  66.  
  67. public double X { get; set; }
  68. public double Y { get; set; }
  69.  
  70. public double CalcDistance(Point other)
  71. {
  72. double a = this.X - other.X;
  73. double b = this.X - other.X;
  74. double distance = Math.Sqrt(GetPow(a) + GetPow(b));
  75. return distance;
  76. }
  77.  
  78. double GetPow(double number)
  79. {
  80. double power = Math.Pow(number, 2);
  81. return power;
  82. }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment