awsmpshk

Untitled

Nov 18th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.18 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApp3
  9. {
  10.     class Program
  11.     {
  12.         struct SPoint : IComparable<SPoint>
  13.         {
  14.             public double x;
  15.             public double y;
  16.  
  17.             public SPoint(double x, double y)
  18.             {
  19.                 this.x = x;
  20.                 this.y = y;
  21.             }
  22.  
  23.             public double Distance()
  24.             {
  25.                 return Math.Sqrt(this.x * this.x + this.y * this.y);
  26.             }
  27.  
  28.             public double Distance(SPoint p)
  29.             {
  30.                 return Math.Sqrt(Math.Pow(this.x - p.x, 2) + Math.Pow(this.y - p.y, 2));
  31.             }
  32.  
  33.             public void Show(StreamWriter outFile)
  34.             {
  35.                 outFile.WriteLine("{0} {1}", this.x, this.y);
  36.             }
  37.  
  38.             public int CompareTo(SPoint p)
  39.             {
  40.                 if (this.Distance() == p.Distance())
  41.                 {
  42.                     return 0;
  43.                 }
  44.                 if (this.Distance() > p.Distance())
  45.                 {
  46.                     return 1;
  47.                 }
  48.                 else
  49.                 {
  50.                     return -1;
  51.                 }
  52.             }
  53.         }
  54.         static void Main(string[] args)
  55.         {
  56.             string inFileName;
  57.             string outFileName;
  58.             using (StreamReader inFile = new StreamReader(inFileName, Encoding.GetEncoding(1251)))
  59.             {
  60.                 using (StreamWriter outFile = new StreamWriter(outFileName, false))
  61.                 {
  62.                     int n = Convert.ToInt32(inFile.ReadLine());
  63.                     int index = 0;
  64.                     SPoint[] points = new SPoint[n];
  65.                     string text;
  66.                     double x, y;
  67.                     while ((text = inFile.ReadLine()) != null)
  68.                     {
  69.                         string[] temp = text.Split(' ');
  70.                         x = Convert.ToDouble(temp[0]);
  71.                         y = Convert.ToDouble(temp[1]);
  72.                         SPoint obj = new SPoint(x, y);
  73.                         points[index] = obj;
  74.                         index++;
  75.                     }
  76.                     double minSumDistance = 1e18;
  77.                     index = -1;
  78.                     for (int i = 0; i < points.Length; ++i)
  79.                     {
  80.                         double sum = 0;
  81.                         for (int j = 0; j < points.Length; ++j)
  82.                         {
  83.                             sum += points[i].Distance(points[j]);
  84.                         }
  85.                         if (sum < minSumDistance)
  86.                         {
  87.                             minSumDistance = sum;
  88.                             index = i;
  89.                         }
  90.                     }
  91.                     if (index != -1)
  92.                     {
  93.                         points[index].Show(outFile);
  94.                     }
  95.                     else
  96.                     {
  97.                         outFile.WriteLine("Nothing was found. You're gay.");
  98.                     }
  99.                 }
  100.             }
  101.         }
  102.     }
  103. }
  104.  
Add Comment
Please, Sign In to add comment