알고리즘 공부/백준

[JAVA 27일차 / 하루 3문제] 10101번, 5073번, 14215번

maverick11471 2025. 3. 2. 11:17

1. 10101번 삼각형 외우기

import java.io.*;

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

        int a = Integer.parseInt(br.readLine());
        int b = Integer.parseInt(br.readLine());
        int c = Integer.parseInt(br.readLine());

        if (a + b + c != 180) {
            System.out.println("Error");
        } else if (a == 60 && b == 60 && c == 60) {
            System.out.println("Equilateral");
        } else if (a == b || b == c || a == c) {
            System.out.println("Isosceles");
        } else {
            System.out.println("Scalene");
        }
    }
}

2. 5073번 삼각형과 세 변

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();

        while (true) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(st.nextToken());
            int b = Integer.parseInt(st.nextToken());
            int c = Integer.parseInt(st.nextToken());

            if (a == 0 && b == 0 && c == 0) {
                break;
            }

            // 가장 긴 변 찾기
            int max = Math.max(a, Math.max(b, c));
            int sum = a + b + c;

            if (max >= sum - max) {
                sb.append("Invalid\n");
            } else if (a == b && b == c) {
                sb.append("Equilateral\n");
            } else if (a == b || b == c || a == c) {
                sb.append("Isosceles\n");
            } else {
                sb.append("Scalene\n");
            }
        }

        System.out.print(sb);
    }
}

3. 14215번 세 막대

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] input = br.readLine().split(" ");
        
        int[] sticks = new int[3];
        sticks[0] = Integer.parseInt(input[0]);
        sticks[1] = Integer.parseInt(input[1]);
        sticks[2] = Integer.parseInt(input[2]);

        Arrays.sort(sticks); // 오름차순 정렬 (a, b, c)

        // 삼각형이 불가능한 경우, 가장 긴 변을 조정
        if (sticks[2] >= sticks[0] + sticks[1]) {
            sticks[2] = sticks[0] + sticks[1] - 1;
        }

        int perimeter = sticks[0] + sticks[1] + sticks[2];
        System.out.println(perimeter);
    }
}
  • 삼각형은 가장 큰 변이 나머지 두 개의 변의 합보다 작아야 한다. 그래서 가장 긴 변의 길이가 나머지 두 변의 합보다 큰 경우 나머지 두 개의 변의 길이의 합보다 1만 줄여서 삼각형이 성립하도록 만드는 공식이다.