본문 바로가기
알고리즘/백준

백준 1002번 자바 java

by 클로드 2020. 12. 15.
반응형

문제

www.acmicpc.net/problem/1002

 

풀이

java의 Math 함수 기능

제곱 : Math.pow();

제곱근 : Math.sqrt();

절대값 : Math.abs();

 

좌표 x1, y1 과 좌표 x2, y2의 거리는 피타고라스 정리로 구할 수 있습니다

$ 두 점의 거리 = \sqrt{(x_{2} - x_{1})^2 + (y_{2} - y_{1})^2} $

 

1. 접점 무한대

두 점이 무한대일때는 좌표와 반지름1(r1), 반지름2(r2)값이 모두 같은 것

x1 = x2, y1 = y2, r1 = r2

 

2. 접점 0개

2-1. 두 원이 접점이 없을때

두점의 거리 > 반지름1 + 반지름2

 

2-2. 원 안에 다른 원이 있지만 접점이 없을때

두점의 거리 < | 반지름1 - 반지름2 |

 

3. 접점 1개

3-1. 두 원의 접점이 1개 일때

두점의 거리 = 반지름1 + 반지름2

 

3-2. 원 안에 다른 원이 있고 접점이 1개일때

두점의 거리 = | 반지름1 - 반지름2 |

 

4. 접점 2개

위에 조건에 맞지 않는다면 접점은 2개

코드

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();

        for (int i = 0; i < T; i++) {
            int x1 = sc.nextInt();
            int y1 = sc.nextInt();
            int r1 = sc.nextInt();
            int x2 = sc.nextInt();
            int y2 = sc.nextInt();
            int r2 = sc.nextInt();

            //무한대
            if (x1 == x2 && y1 == y2 && r1 == r2) {
                System.out.println(-1);
                continue;
            }

            double distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));

            //0
            if (distance > r1 + r2) {
                System.out.println(0);
                continue;
            }

            if (distance < Math.abs(r1 - r2)) {
                System.out.println(0);
                continue;
            }

            //1
            if (distance == r1 + r2) {
                System.out.println(1);
                continue;
            }

            if (distance == Math.abs(r1 - r2)) {
                System.out.println(1);
                continue;
            }

            //2
            System.out.println(2);
        }
    }
}

 

반응형

'알고리즘 > 백준' 카테고리의 다른 글

백준 10814 문제 자바 java  (0) 2021.01.12
백준 2108번 자바 java  (0) 2020.12.24
백준 10989번 자바 java  (0) 2020.12.23
백준 2751번 자바 java  (0) 2020.12.17
백준 2447번 자바 java  (0) 2020.12.17

댓글