fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ClosestStars
  6. {
  7. class Star : IComparable<Star>
  8. {
  9. public readonly long X;
  10. public readonly long Y;
  11. public readonly long Z;
  12. public readonly long DISTANCE;
  13.  
  14. public Star(int x, int y, int z)
  15. {
  16. X = (long)x;
  17. Y = (long)y;
  18. Z = (long)z;
  19. DISTANCE = X * X + Y * Y + Z * Z;
  20. }
  21.  
  22. public double Distance(Star other)
  23. {
  24. long dx = X - other.X;
  25. long dy = Y - other.Y;
  26. long dz = Z - other.Z;
  27. return Math.Sqrt(dx * dx + dy * dy + dz * dz);
  28. }
  29.  
  30. public override string ToString()
  31. {
  32. return String.Format("{0} {1} {2}", X, Y, Z);
  33. }
  34.  
  35. public int CompareTo(Star other)
  36. {
  37. return DISTANCE < other.DISTANCE ? -1 :
  38. Convert.ToInt32(DISTANCE != other.DISTANCE);
  39. }
  40. }
  41.  
  42. class StarPair : IComparable<StarPair>
  43. {
  44. public readonly Star STAR_1;
  45. public readonly Star STAR_2;
  46. private readonly double DISTANCE;
  47.  
  48. public StarPair(Star star1, Star star2)
  49. {
  50. STAR_1 = star1;
  51. STAR_2 = star2;
  52. DISTANCE = STAR_1.Distance(STAR_2);
  53. }
  54.  
  55. public override string ToString()
  56. {
  57. return String.Format(
  58. "Star1: {0}\nStar2: {1}\nDistance: {2}",
  59. STAR_1, STAR_2, DISTANCE);
  60. }
  61.  
  62. public int CompareTo(StarPair other)
  63. {
  64. return DISTANCE < other.DISTANCE ? -1 : 1;
  65. }
  66. }
  67.  
  68. class Program
  69. {
  70. static void Main(string[] args)
  71. {
  72. Random rnd = new Random();
  73. Star[] stars = new Star[1000000];
  74. int i;
  75. for (i = 0; i < 1000000; i++)
  76. {
  77. stars[i] = new Star(
  78. rnd.Next(1, 1000000),
  79. rnd.Next(1, 1000000),
  80. rnd.Next(1, 1000000));
  81. }
  82. Array.Sort(stars);
  83. StarPair[] pairs = new StarPair[999999];
  84. for (i = 0; i < 999999; i++)
  85. {
  86. pairs[i] = new StarPair(stars[i], stars[i + 1]);
  87. }
  88. Console.WriteLine(pairs.Min());
  89. }
  90. }
  91. }
Success #stdin #stdout 3.69s 26080KB
stdin
Standard input is empty
stdout
Star1: 113070 321437 73697
Star2: 113423 321243 74067
Distance: 546.941495957292