Advertisement
Guest User

Untitled

a guest
Apr 21st, 2015
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1.  
  2. package hello;
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.util.ArrayList;
  6. import java.util.Scanner;
  7.  
  8.  
  9. public class sasiedzi
  10. {
  11. public static class Point
  12. {
  13. // public int x;
  14. // public int y;
  15. // public int z;
  16. // public int a;
  17. ArrayList<int> punkty;
  18.  
  19. }
  20. public static void main(String [ ] args)
  21. {
  22. Scanner co = new Scanner(System.in);
  23. //String filename = co.nextLine();
  24. String filename="D:/sa.txt";
  25. try
  26. {
  27. BufferedReader reader = new BufferedReader(new FileReader(filename));
  28. ArrayList<Point> pts = new ArrayList<Point>();
  29. String line;
  30.  
  31. while((line = reader.readLine()) != null)
  32. {
  33. String [] data = line.split(" ");
  34.  
  35. Point pt = new Point();
  36. for(int i =0; i < data.length; i++)
  37. System.out.println(data[i]);
  38.  
  39. // pt.x = Integer.valueOf(data[0]);
  40. // pt.y = Integer.valueOf(data[1]);
  41. // pt.z = Integer.valueOf(data[2]);
  42.  
  43. pts.add(pt);
  44. }
  45.  
  46. reader.close();
  47. System.out.println("Podaj x");
  48. String xStr = co.nextLine();
  49. System.out.println("Podaj y");
  50. String yStr = co.nextLine();
  51. System.out.println("Podaj z");
  52. String zStr = co.nextLine();
  53.  
  54. Point pt = new Point();
  55. pt.x = Integer.valueOf(xStr);
  56. pt.y = Integer.valueOf(yStr);
  57. pt.z = Integer.valueOf(zStr);
  58.  
  59. double distance = Float.MAX_VALUE;
  60. Point neighboor = null;
  61. for(Point p : pts)
  62. {
  63. double dx = p.x - pt.x;
  64. double dy = p.y - pt.y;
  65. double dz = p.z - pt.z;
  66. double d = Math.sqrt(dx*dx+dy*dy+dz*dz);
  67. if(d < distance)
  68. {
  69. distance = d;
  70. neighboor = p;
  71. }
  72. }
  73.  
  74. System.out.printf("Najbliższy sąsiad to %d,%d,%d w odległości %f",neighboor.x,neighboor.y,neighboor.z,distance);
  75. co.nextLine();
  76. co.close();
  77.  
  78. }
  79. catch(Exception exc)
  80. {
  81. System.out.println("nie można otworzyć pliku");
  82. return;
  83. }
  84.  
  85. }
  86. private static String valueOf(String string) {
  87. // TODO Auto-generated method stub
  88. return null;
  89. }
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement