백준 Right Triangle

KIMYEONGJUN·2026년 4월 10일
post-thumbnail

문제

내가 생각했을때 문제에서 원하는부분

The first line has a positive integer T, T <= 100000, denoting the number of test cases.
This is followed by each test case per line.
Each test case consists of a line containing 3 integers a,b and c denoting the sides of a triangle.
All of these sides will be between 1 and 100, inclusive. The sides a,b and c can be given in any order.

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.

내가 이 문제를 보고 생각해본 부분

입력 처리
첫 줄에서 테스트 케이스 수 T를 받는다.
각 테스트 케이스마다 한 줄에 3개의 변 길이를 공백을 기준으로 받아 배열에 저장한다.
정렬과 변수 분리
입력 받은 변 배열을 오름차순으로 정렬하여 가장 큰 변이 sides[2]에 위치하도록 한다.
이후 a, b, c에 각각 작은 변부터 큰 변 순서대로 할당한다.
피타고라스 정리 검사
가장 긴 변 c의 제곱과 나머지 두 변 a와 b 제곱의 합을 비교한다.
같으면 "YES" 아니면 "NO"를 결과로 설정한다.
출력
형식에 맞춰 "Case #x: M" 으로 출력한다. 
여기서 x는 테스트 케이스 번호이고, M은 직각삼각형이면 "YES", 아니면 "NO"이다.

코드로 구현

package baekjoon.baekjoon_34;

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

// 백준 9723번 문제
public class Main1353 {
    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 = 1; i <= T; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            int[] sides = new int[3];

            // 세 변 입력 받기
            for (int j = 0; j < 3; j++) {
                sides[j] = Integer.parseInt(st.nextToken());
            }

            // 배열 정렬 (오름차순)
            Arrays.sort(sides);

            // 피타고라스 정리 확인
            int a = sides[0];
            int b = sides[1];
            int c = sides[2];

            String result = (c * c == a * a + b * b) ? "YES" : "NO";
            System.out.printf("Case #%d: %s\n", i, result);
        }

        br.close();
    }
}

마무리

코드와 설명이 부족할수 있습니다. 코드를 보시고 문제가 있거나 코드 개선이 필요한 부분이 있다면 댓글로 말해주시면 감사한 마음으로 참고해 코드를 수정 하겠습니다.

profile
Junior backend developer

0개의 댓글