Algorithm/BOJ(Baekjoon Online Judge)

[백준 - 1004번] 어린 왕자 - Java

koucop 2020. 9. 5. 11:20

 

이번에는 BOJ의 1004번 문제 "어린 왕자" 를 풀어보도록 하자

성공한 코드는 다음과 같다.

import java.io.*;

public class Main{
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        String line = br.readLine();
        int t = Integer.parseInt(line);
        int x1, y1, x2, y2;
        double dA, dB;
        int count = 0;
        for(int i = 0; i < t; i++){
            count = 0;
            line = br.readLine();
            x1 = Integer.parseInt(line.split(" ")[0]);
            y1 = Integer.parseInt(line.split(" ")[1]);
            x2 = Integer.parseInt(line.split(" ")[2]);
            y2 = Integer.parseInt(line.split(" ")[3]);
            line = br.readLine();
            int n = Integer.parseInt(line);
            for(int j = 0; j < n; j++){
                line = br.readLine();
                int cx = Integer.parseInt(line.split(" ")[0]);
                int cy = Integer.parseInt(line.split(" ")[1]);
                int r = Integer.parseInt(line.split(" ")[2]);
                dA = Math.sqrt(Math.pow((double)x1 - (double)cx, 2) + Math.pow((double)y1 - (double)cy, 2));
                dB = Math.sqrt(Math.pow((double)x2 - (double)cx, 2) + Math.pow((double)y2 - (double)cy, 2));
                if(dA < (double)r && dB < (double)r){

                }else if(dA < r){
                    count++;
                }else if(dB < r){
                    count++;
                }
            }
            bw.write(count + "\n");
        }

        bw.flush();
        br.close();
        bw.close();
    }
}

 

 

문제 : https://www.acmicpc.net/problem/1004

 

1004번: 어린 왕자

입력의 첫 줄에는 테스트 케이스의 개수 T가 주어진다. 그 다음 줄부터 각각의 테스트케이스에 대해 첫째 줄에 출발점 (x1, y1)과 도착점 (x2, y2)이 주어진다. 두 번째 줄에는 행성계의 개수 n이 주��

www.acmicpc.net

 

 

혹시 코드에 이상한 부분이나 틀린 부분이 있던지, 이해가 안가는 부분이 있다면 댓글로 알려주세요.