Advertisement
Guest User

klaster

a guest
May 29th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5.  
  6. import java.util.Comparator;
  7.  
  8. class Point2D {
  9. long id;
  10. float x;
  11. float y;
  12. float dis;
  13. Point2D(long a,float b,float c){
  14. id=a;
  15. x=b;
  16. y=c;
  17. dis=0;
  18.  
  19.  
  20. }
  21. public int compareTo(Point2D a){
  22. if(a.dis>=this.dis)
  23. return 1;
  24. else return -1;
  25.  
  26. }
  27. }
  28.  
  29.  
  30. class AVGComparator implements Comparator<Point2D> {
  31. public int compare(Point2D m1, Point2D m2) {
  32.  
  33.  
  34. if(m1.dis<m2.dis)
  35. return -1;
  36. else return 1;
  37.  
  38. }
  39. }
  40.  
  41. class Cluster<T> {
  42. ArrayList<Point2D> lista;
  43. Cluster(){
  44. lista=new ArrayList<Point2D>();
  45. }
  46.  
  47. public void addItem(Point2D point2d) {
  48. lista.add(point2d);
  49.  
  50. }
  51.  
  52. public void near(int idd, int top) {
  53. Point2D a=null;
  54. for(int i=0;i<lista.size();i++){
  55. if(lista.get(i).id==idd)
  56. {
  57. a=lista.get(i);
  58.  
  59. }
  60. }
  61. for(int i=0;i<lista.size();i++){
  62. Point2D c=lista.get(i);
  63. float dist=presmetaj(a,c);
  64. c.dis=dist;
  65.  
  66. }
  67. Collections.sort(lista,new AVGComparator());
  68. for(int i=1;i<=top;i++)
  69. { String aaa=String.format("%d. %d -> %.3f",i,lista.get(i).id,lista.get(i).dis);
  70. System.out.println(aaa);
  71. }
  72.  
  73. }
  74.  
  75. private float presmetaj(Point2D a, Point2D c) {
  76. return (float) Math.sqrt((a.x-c.x)*(a.x-c.x)+(a.y-c.y)*(a.y-c.y));
  77.  
  78. }
  79. }
  80.  
  81.  
  82. /**
  83. * January 2016 Exam problem 2
  84. */
  85. public class ClusterTest {
  86. public static void main(String[] args) {
  87. Scanner scanner = new Scanner(System.in);
  88. Cluster<Point2D> cluster = new Cluster<>();
  89. int n = scanner.nextInt();
  90. scanner.nextLine();
  91. for (int i = 0; i < n; ++i) {
  92. String line = scanner.nextLine();
  93. String[] parts = line.split(" ");
  94. long id = Long.parseLong(parts[0]);
  95. float x = Float.parseFloat(parts[1]);
  96. float y = Float.parseFloat(parts[2]);
  97. cluster.addItem(new Point2D(id, x, y));
  98. }
  99. int id = scanner.nextInt();
  100. int top = scanner.nextInt();
  101. cluster.near(id, top);
  102. scanner.close();
  103. }
  104. }
  105.  
  106. // your code here
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement