Algorithm/BOJ(Baekjoon Online Judge)

[백준 - 9723번] Right Triangle - Java //Wello Horld //

koucop 2020. 3. 8. 11:45

 

 

이번에는 BOJ의 9723번 문제 "Right Triangle" 을 풀어보도록 하자

 

 

입력으로 첫째 줄에 테스트 케이스 T가 주어지고, 그 다음 줄부터 T줄 만큼 a, b, c 가 주어진다.

출력으로 여기서 a, b, c 로 이루어진 선분을 서로 붙였을 때, 직각 삼격형이 된다면 YES 를 아니면 NO 를 출력하면 되는 문제이다.

성공한 코드는 아래와 같다.

 

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

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));

        int T = Integer.parseInt(br.readLine());
        for (int i = 0; i < T; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            int d1 = Integer.parseInt(st.nextToken());
            int d2 = Integer.parseInt(st.nextToken());
            int d3 = Integer.parseInt(st.nextToken());
            boolean chk = false;
            if(d1 * d1 == d2 * d2 + d3 * d3){
                chk = true;
            } else if(d2 * d2 == d3 * d3 + d1 * d1){
                chk = true;
            } else if(d3 * d3 == d1 * d1 + d2 * d2){
                chk = true;
            }
            if(chk){
                bw.write("Case #" + (i + 1) + ": YES\n");
            } else {
                bw.write("Case #" + (i + 1) + ": NO\n");
            }
        }

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

 

 

 

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

 

9723번: Right Triangle

For each test case, the output contains a line in the format Case #x: M, where x is the case number (starting from 1) and M is “YES” when the given triangle is a right triangle or “NO” otherwise. Note that the quotes are not required to be outputted. 

www.acmicpc.net

 

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