Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- class Point2D {
- long id;
- float x;
- float y;
- float dis;
- Point2D(long a,float b,float c){
- id=a;
- x=b;
- y=c;
- dis=0;
- }
- public int compareTo(Point2D a){
- if(a.dis>=this.dis)
- return 1;
- else return -1;
- }
- }
- class AVGComparator implements Comparator<Point2D> {
- public int compare(Point2D m1, Point2D m2) {
- if(m1.dis<m2.dis)
- return -1;
- else return 1;
- }
- }
- class Cluster<T> {
- ArrayList<Point2D> lista;
- Cluster(){
- lista=new ArrayList<Point2D>();
- }
- public void addItem(Point2D point2d) {
- lista.add(point2d);
- }
- public void near(int idd, int top) {
- Point2D a=null;
- for(int i=0;i<lista.size();i++){
- if(lista.get(i).id==idd)
- {
- a=lista.get(i);
- }
- }
- for(int i=0;i<lista.size();i++){
- Point2D c=lista.get(i);
- float dist=presmetaj(a,c);
- c.dis=dist;
- }
- Collections.sort(lista,new AVGComparator());
- for(int i=1;i<=top;i++)
- { String aaa=String.format("%d. %d -> %.3f",i,lista.get(i).id,lista.get(i).dis);
- System.out.println(aaa);
- }
- }
- private float presmetaj(Point2D a, Point2D c) {
- return (float) Math.sqrt((a.x-c.x)*(a.x-c.x)+(a.y-c.y)*(a.y-c.y));
- }
- }
- /**
- * January 2016 Exam problem 2
- */
- public class ClusterTest {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Cluster<Point2D> cluster = new Cluster<>();
- int n = scanner.nextInt();
- scanner.nextLine();
- for (int i = 0; i < n; ++i) {
- String line = scanner.nextLine();
- String[] parts = line.split(" ");
- long id = Long.parseLong(parts[0]);
- float x = Float.parseFloat(parts[1]);
- float y = Float.parseFloat(parts[2]);
- cluster.addItem(new Point2D(id, x, y));
- }
- int id = scanner.nextInt();
- int top = scanner.nextInt();
- cluster.near(id, top);
- scanner.close();
- }
- }
- // your code here
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement