sm 기술 블로그

102. 1002(터렛) 본문

문제/백준_자바

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로 따로 식을 구현하지 않아도 된다.

'문제 > 백준_자바' 카테고리의 다른 글

104. 1358(하키)  (0) 2022.06.23
103. 1004(어린 왕자)  (0) 2022.06.23
101. 택시 기하학(3053)  (0) 2022.06.22
100. 2477(참외 밭)  (0) 2022.06.22
99. 4153(직각삼각형)  (0) 2022.06.21
Comments