코딩테스트/백준

백준 1002 : 터렛 _자바 Java

플래시🦥 2023. 1. 12.
반응형

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

 

1002번: 터렛

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

www.acmicpc.net


import java.io.*;
import java.util.*;

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

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(br.readLine());
		
		for(int i=0;i<N;i++) {
			String str= br.readLine();
			find(str);
		}
		
	}
	private static void find(String str) {
		int res=0;
		StringTokenizer st = new StringTokenizer(str," ");

		int x1=Integer.parseInt(st.nextToken());
		int y1=Integer.parseInt(st.nextToken());
		int r1=Integer.parseInt(st.nextToken());
		int x2=Integer.parseInt(st.nextToken());
		int y2=Integer.parseInt(st.nextToken());
		int r2=Integer.parseInt(st.nextToken());
		
		double dist = Math.pow(x2-x1,2)+Math.pow(y2-y1,2);
		
		//중점이 같으면서 반지름도 같을 때
		if(x1 == x2 && y1 == y2 && r1 == r2) {
			res= -1;
		}
		
		//원이 서로 외부일 때  
		else if(dist > Math.pow(r1 + r2, 2)) {
			res= 0;
		}
 
		//원 안에 원이 있으나 내접하지 않을 때 
		else if(dist < Math.pow(r2 - r1, 2)) {
			res= 0;
		}
		
		//내접할 때 
		else if(dist == Math.pow(r2 - r1, 2)) {
			res= 1;
		}
        
		
		//외접할 때 
		else if(dist == Math.pow(r1 + r2, 2)) {
			res= 1;
		}
		
		else {
			res= 2;
		}		
		
		
		
		System.out.println(res);
	}
		
}
728x90
반응형

댓글