문제/백준_자바
102. 1002(터렛)
sm_hope
2022. 6. 22. 21:44
import java.util.*;
import java.io.*;
class Main{
public static void main(String args[])throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int T = Integer.parseInt(br.readLine());
for(int i = 0; i< T; i++) {
String[] sBits = br.readLine().split(" ");
int x1 = Integer.parseInt(sBits[0]);
int y1 = Integer.parseInt(sBits[1]);
int r1 = Integer.parseInt(sBits[2]);
int x2 = Integer.parseInt(sBits[3]);
int y2 = Integer.parseInt(sBits[4]);
int r2 = Integer.parseInt(sBits[5]);
// 무한대일 경우 : -1
if(x1==x2 && y1==y2 && r1==r2) {
sb.append("-1").append("\n");
}
// 접점이 없는경우 : 0
else if(Math.pow(x2-x1,2) + Math.pow(y2-y1,2) > Math.pow(r2+r1,2) || Math.pow(x2-x1,2) + Math.pow(y2-y1,2) < Math.pow(r2-r1,2)) {
sb.append("0").append("\n");
}
// 접점이 하나 있는경우 : 1
else if(Math.pow(x2-x1,2) + Math.pow(y2-y1,2) == Math.pow(r2+r1,2) || Math.pow(x2-x1,2) + Math.pow(y2-y1,2) == Math.pow(r2-r1,2)) {
sb.append("1").append("\n");
}
// 그외 : 2
else {
sb.append("2").append("\n");
}
}
System.out.println(sb);
}
}
문제요약
조규현의 좌표에서 규현이가 계산한 류재명의 거리와 백승환의 좌표에서 승환이가 계산한 류재명의 거리에서 류재명이 있을 수 있는 위치의 수는?
설명
두 원의 각 위치와 거리를 구하라는 문제와 같다.
1. 무한대가 나올경우 (두 점과 두 거리가 일치한다.) : -1
2. 값이 0개일 경우 (두 원이 닿지 않는다.) : 0
3. 값이 1개일 경우 (두 원이 한 점에서 닿는다.) : 1
4. 값이 2개일 경우 (두 원이 두점에서 닿는다.) : 2
이와 같은 성질을 알고 있다면 구현하는데 어렵지 않다.
마지막 4번같은 경우는 else로 따로 식을 구현하지 않아도 된다.