JAVA/Coding Test

[JAVA] 백준 1002 터렛_기하1

오늘도개발 2023. 3. 7. 10:50

https://www.acmicpc.net/problem/1002

 

1002번: 터렛

각 테스트 케이스마다 류재명이 있을 수 있는 위치의 수를 출력한다. 만약 류재명이 있을 수 있는 위치의 개수가 무한대일 경우에는 -1을 출력한다.

www.acmicpc.net

 

접근 : 

 

 - 테스트 케이스 갯수 n 을 입력받는다.

 

 - 두 원의 원점의 좌표 와 반지름을 입력 받는다.

 

 - 두개의 원의 접점을 출력한다.

 

 - 두개의 원이 존재할 경우는 다음과 같다.

 

 

 코드 구현 : 

 

import java.io.*;

public class Main {

	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuffer sb = new StringBuffer();
		int n = Integer.parseInt(br.readLine());
		String[] inputs;
		int x1, y1, r1 = 0;
		int x2, y2, r2 = 0;
		double pow_distance = 0;
		double temp_r = 0;

		// 각각의 변 길이 입력, 가장 큰 사각형의 x, y값 저장
		for (int i = 0; i < n; i++) {
			inputs = br.readLine().split(" ");
			x1 = Integer.parseInt(inputs[0]);
			y1 = Integer.parseInt(inputs[1]);
			r1 = Integer.parseInt(inputs[2]);
			x2 = Integer.parseInt(inputs[3]);
			y2 = Integer.parseInt(inputs[4]);
			r2 = Integer.parseInt(inputs[5]);

			pow_distance = Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2);
			temp_r = Math.pow((r1 - r2), 2);

			// 원의 중심이 일치하는 경우
			if (x1 == x2 && y1 == y2) {
				if (r1 == r2) sb.append(-1).append("\n");
				else sb.append(0).append("\n");

			// 한개의 원이 다른 원 안에 있는 경우
			} else if (temp_r > pow_distance) {
				sb.append(0).append("\n");

			// 한개의 원이 다른 원 안에 있으면서 1점에서 만나는 경우
			} else if (temp_r == pow_distance) {
				sb.append(1).append("\n");

			// 한개의 원이 다른 원 밖에 있는 경우
			} else {
				temp_r = Math.pow((r1 + r2), 2);
				// 2 점에서 만나는 경우
				if (temp_r > pow_distance) {
					sb.append(2).append("\n");

				// 1점에서 만나는 경우
				} else if (temp_r == pow_distance) {
					sb.append(1).append("\n");

				// 만나는 않는 경우
				} else {
					sb.append(0).append("\n");
				}
			}
		}

		System.out.println(sb);
		br.close();
	}
}