본문 바로가기

백준 문제풀기/JAVA

[백준 1002 JAVA 자바] 터렛

갑자기 실버등급의 문제가 나왔습니다

(백준에는 브론즈~실버~골드~~~ 순서대로 난이도가 있는것 같습니다)

 

이 문제의 핵심은

두 원의 교점입니다

 

두 원의 중점과

두 원의 반지름을 입력하여

교점이 몇개인지 구하는 문제입니다

1번 2번 같은 경우는 교점이 1개

3번 같은 경우는 교점이 2개

4번 5번 같은 경우는 교점이 없습니다

(+ 그림에는 없지만 두 원이 똑같은 경우도 생각해줍시다)

 

코딩은

1. 두 원의 중점끼리의 거리와

2. 두 원의 반지름끼리의 거리를

3. 적당히 비교해서 답을 출력합시다

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {

    public static void main(String[] args) throws Exception {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        
        int circle1[] = new int[3];
        int circle2[] = new int[3];
        
        double distance, tc;
        String[] temp;

        tc = Integer.parseInt(br.readLine());

        while(tc > 0){
            temp = br.readLine().split(" ");

            for(int i = 0; i < circle1.length; i++){
                circle1[i] = Integer.parseInt(temp[i]);
                circle2[i] = Integer.parseInt(temp[i + 3]);
            }

            distance = Math.sqrt((circle1[0] - circle2[0]) * (circle1[0] - circle2[0]) + (circle1[1] - circle2[1]) * (circle1[1] - circle2[1]));

            if(circle1[2] + circle2[2] > distance && distance > Math.abs(circle1[2] - circle2[2]))
                sb.append("2\n");
            else if(circle1[2] + circle2[2] == distance || distance == Math.abs(circle1[2] - circle2[2])) {
                if(Arrays.equals(circle1, circle2))
                    sb.append("-1\n");
                else
                   sb.append("1\n");
            }
            else
                sb.append("0\n");

            tc -= 1;
        }
        System.out.println(sb);
    }
}

1000,1001번에선 사용하지 않은

BufferedReader와 StringBuilder가 나옵니다.

 

BufferedReader는 Scanner보다 빠르게 입력할 수 있는 친구입니다

throws Exception을 추가해줘야 합니다

 

StringBuilder는 출력하고싶은 String을 포장해주는 틀입니다

StringBuilder로 묶고

하나의 System.out.print()로 출력합니다

'백준 문제풀기 > JAVA' 카테고리의 다른 글