1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class CodingPractice02 { public static boolean isOverlapped(Circle c1, Circle c2){ double sumOfR = c1.getR() + c2.getR(); double distance = Math.sqrt(Math.pow(c1.getX()-c2.getX(),2) + Math.pow(c1.getY() - c2.getY(), 2)); if(distance < sumOfR){ return true; }else{ return false; } } public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); int theNumberOfCircle=-1; // set the number of circles while(theNumberOfCircle < 0){ System.out.println("Please input the number of circles."); theNumberOfCircle = scan.nextInt(); if(theNumberOfCircle<0){ System.out.print("Worng value. "); } } List<Circle> circles = new ArrayList<Circle>(theNumberOfCircle); List<Integer> circlesId = new ArrayList<Integer>(theNumberOfCircle); // input circles for(int i = 0 ; i < theNumberOfCircle ; i++){ Circle tempCircle = new Circle(scan.nextInt(), scan.nextDouble(), scan.nextDouble(), scan.nextDouble()); // validation check // range of circle if( (1 > tempCircle.getId() || tempCircle.getId() > 300) || (-10000 > tempCircle.getX() || tempCircle.getX() > 10000) || (-10000 > tempCircle.getY() | tempCircle.getY() > 10000) || (0 >= tempCircle.getR() || tempCircle.getR() > 10000) ){ System.out.println("Worng value"); i--; continue; }else if(i > 0){ // ID duplication check if(circlesId.contains(tempCircle.getId())){ System.out.println("Worng value"); i--; continue; } } circles.add(tempCircle); circlesId.add(tempCircle.getId()); } // printing overlapped circles for(int i = 0 ; i < circles.size() ; i ++){ for(int j = i ; j < circles.size() ; j++){ if(i==j){ continue; }else{ if(isOverlapped(circles.get(i), circles.get(j))){ System.out.println(circles.get(i).getId() + ", " + circles.get(j).getId()); } } } } } } class Circle{ private int id; private double x; private double y; private double r; public Circle() { super(); } public Circle(int id, double x, double y, double r) { super(); this.id = id; this.x = x; this.y = y; this.r = r; } public int getId() { return id; } public void setId(int id) { this.id = id; } public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } public double getR() { return r; } public void setR(double r) { this.r = r; } } | cs |
반응형
'잔잔바리 > CodingPractice' 카테고리의 다른 글
[Java/CodingPractice]04. CodingPractice04 (0) | 2016.08.01 |
---|---|
[Java/CodingPractice]03. CodingPractice03 (0) | 2016.07.30 |
[Java/CodingPractice]01. CodingPractice01 (0) | 2016.06.02 |