๊ธฐ๋ก๋ฐฉ

BOJ_1002 : ํ„ฐ๋ › ๋ณธ๋ฌธ

CodingTest/Java

BOJ_1002 : ํ„ฐ๋ ›

Soom_1n 2022. 12. 7. 10:19

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

1002๋ฒˆ: ํ„ฐ๋ ›

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค ๋ฅ˜์žฌ๋ช…์ด ์žˆ์„ ์ˆ˜ ์žˆ๋Š” ์œ„์น˜์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ ๋ฅ˜์žฌ๋ช…์ด ์žˆ์„ ์ˆ˜ ์žˆ๋Š” ์œ„์น˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋ฌดํ•œ๋Œ€์ผ ๊ฒฝ์šฐ์—๋Š” -1์„ ์ถœ๋ ฅํ•œ๋‹ค.

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ๋‘ ์›์˜ ์ขŒํ‘œ์™€ ๋ฐ˜์ง€๋ฆ„์„ ์ž…๋ ฅ๋ฐ›๊ณ  ์›์˜ ์ ‘์ ์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
    • 0 : ์ ‘์ ์ด ์—†์Œ
    • 1 : ๋‚ด์ ‘ ํ˜น์€ ์™ธ์ ‘์œผ๋กœ 1๊ฐœ์˜ ์ ‘์ 
    • 2 : 2๊ฐœ์˜ ์ ‘์ 
    • -1 : ์ ‘์ ์ด ๋ฌดํ•œ(์ผ์น˜ํ•˜๋Š” ๋‘ ์›)

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine());

        for (int i = 0; i < t; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            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 len = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));

            if (x1 == x2 && y1 == y2 && r1 == r2) { // ๋‘ ์›์ด ์ผ์น˜ํ•จ
                System.out.println(-1);
            }
            else if (len == r1 + r2 || len == Math.abs(r1 - r2)) { // ๋‘ ์›์ด ํ•˜๋‚˜์˜ ์ ‘์ ์„ ๊ฐ€์ง
                System.out.println(1);
            }
            else if (len < r1 + r2 && len > Math.abs(r1 - r2)) { // ๋‘ ์›์ด ๋‘ ์ ‘์ ์„ ๊ฐ€์ง
                System.out.println(2);
            }
            else { // ๋‘ ์›์ด ๋งŒ๋‚˜์ง€ ์•Š์Œ
                System.out.println(0);
            }
        }
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ๋‘ ์›์˜ ์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ , ๋‘ ์  ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์˜ค์ฐจ๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•ด doubleํ˜•์œผ๋กœ ์ž…๋ ฅ๋ฐ›์•˜๋‹ค.
  • ๋‘ ์›์ด ๋งŒ๋‚˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋Š” ๋”ฐ๋กœ ๋–จ์–ด์ง, ์ค‘์‹ฌ์ด ๊ฐ™์€๋ฐ ๋ฐ˜์ง€๋ฆ„์ด ๋‹ค๋ฆ„, ์ค‘์‹ฌ์€ ๋‹ค๋ฅธ๋ฐ ํ•œ ์› ์•ˆ์— ๋‹ค๋ฅธ ์›์ด ํฌํ•จ๋˜์–ด์žˆ์Œ์œผ๋กœ 3๊ฐ€์ง€ ๊ฒฝ์šฐ๋ฅผ ๋”ฐ์ ธ์•ผํ•ด์„œ else๋ฌธ์œผ๋กœ ์ฒ˜๋ฆฌํ–ˆ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ๋‘ ์›์˜ ๊ด€๊ณ„๋ฅผ ์ƒ๊ฐํ•ด๋ด์•ผํ•˜๋Š” ์ˆ˜ํ•™, ๊ธฐํ•˜ํ•™ ๋ฌธ์ œ์˜€๋Š”๋ฐ ์ˆ˜ํ•™ ์›๋ฆฌ๋ฅผ ์ฐพ์•„๋ณด๊ฒŒ ๋˜์–ด์„œ ํฅ๋ฏธ๋กญ๊ฒŒ ํ’€์ดํ–ˆ๋‹ค.
  • ํ•˜๋‚˜์˜ ์ ‘์ ์„ ๊ฐ€์งˆ๋•Œ ์™ธ์ ‘๋งŒ ์ƒ๊ฐํ•˜๊ณ  ๋‚ด์ ‘์˜ ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š์•„์„œ ํ‹€๋ ธ์—ˆ๋‹ค.
  • ์ˆ˜ํ•™์ ์ธ ์›๋ฆฌ๋ฅผ ์ฐพ์•„๋ณด๋ ค๊ณ  ๋‘ ์›์˜ ์œ„์น˜๊ด€๊ณ„ ํฌ์ŠคํŒ…์„ ์ฐพ์•„๋ณด์•˜๊ณ , ์ฝ”๋“œ์ ์œผ๋กœ ์–ด๋–ค ์กฐ๊ฑด์ด ํ•„์š”ํ•œ์ง€ ์งˆ๋ฌธ ์„ค๋ช…๊ธ€๋„ ์ฐพ์•„๋ณด์•˜๋‹ค.
  • ์ฒ˜์Œ ๋ฌธ์ œ๋ฅผ ๋ณด์•˜์„๋•Œ๋Š” ์ดํ•ด๊ฐ€ ์ž˜ ๋˜์ง€ ์•Š์•„์„œ ์˜ˆ์‹œ ์ž…๋ ฅ์„ ๊ทธ๋ ค๋ณด๊ณ  ๋‘ ์›์˜ ์œ„์น˜๊ด€๊ณ„ ๋ฌธ์ œ๋ผ๋Š” ๊ฑธ ์•Œ๊ฒŒ๋˜์—ˆ๋‹ค.

728x90

'CodingTest > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_15650 : N๊ณผ M (2)  (0) 2022.12.09
BOJ_15649 : N๊ณผ M (1)  (0) 2022.12.08
BOJ_14425 : ๋ฌธ์ž์—ด ์ง‘ํ•ฉ  (0) 2022.12.06
BOJ_1269 : ๋Œ€์นญ ์ฐจ์ง‘ํ•ฉ  (0) 2022.12.06
BOJ_9733 : ๊ฟ€๋ฒŒ  (0) 2022.12.06