Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. /**
  5. * Created by BoldijarPaul on 31/01/2017.
  6. */
  7. public class Main {
  8.  
  9. public static class Location implements Comparable {
  10. int x;
  11. int y;
  12. double distance;
  13. int initialIndex;
  14.  
  15. Location(int x, int y) {
  16. this.x = x;
  17. this.y = y;
  18. }
  19.  
  20. @Override
  21. public int compareTo(Object o) {
  22. if (o == null || !(o instanceof Location)) {
  23. return 0;
  24. }
  25. Location location = (Location) o;
  26. if (distance < location.distance) {
  27. return -1;
  28. }
  29. return 1;
  30. }
  31. }
  32.  
  33. public static class TestCase {
  34. Location userLocation;
  35. TreeSet<Location> stores;
  36. }
  37.  
  38. private static double distance(Location location1, Location location2) {
  39. return Math.sqrt((location1.x - location2.x) * (location1.x - location2.x) +
  40. (location1.y - location2.y) * (location1.y - location2.y));
  41. }
  42.  
  43. private static Location locationFromString(String locationString) {
  44. String[] coords = locationString.split("\\s+");
  45. return new Location(Integer.valueOf(coords[0]), Integer.valueOf(coords[1]));
  46. }
  47.  
  48. public static void main(String[] args) throws IOException {
  49. List<TestCase> testCaseList = new ArrayList<>();
  50. BufferedReader reader = new BufferedReader(new FileReader(new File("task1-test-input.txt")));
  51. int testCases = Integer.valueOf(reader.readLine());
  52. reader.readLine();
  53. for (int i = 1; i <= testCases; i++) {
  54. TestCase testCase = new TestCase();
  55. testCase.stores = new TreeSet<>();
  56. testCase.userLocation = locationFromString(reader.readLine());
  57. int storesCount = Integer.valueOf(reader.readLine());
  58. for (int j = 1; j <= storesCount; j++) {
  59. Location storeLocation = locationFromString(reader.readLine());
  60. storeLocation.distance = distance(storeLocation, testCase.userLocation);
  61. storeLocation.initialIndex = j;
  62. testCase.stores.add(storeLocation);
  63. }
  64. testCaseList.add(testCase);
  65. reader.readLine();
  66. }
  67. reader.close();
  68. outputTestCases(testCaseList);
  69.  
  70. }
  71.  
  72. private static void outputTestCases(List<TestCase> testCaseList) throws FileNotFoundException, UnsupportedEncodingException {
  73. PrintWriter printWriter = new PrintWriter("task1-test-output.txt", "UTF-8");
  74. for (int i = 0, testCaseListSize = testCaseList.size(); i < testCaseListSize; i++) {
  75. TestCase testCase = testCaseList.get(i);
  76. Location closestLocation = testCase.stores.first();
  77. printWriter.println(String.format("Case #%d: %d %.3f", i + 1, closestLocation.initialIndex, closestLocation.distance));
  78. }
  79. printWriter.close();
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement