본문 바로가기
Problem Solving/Baekjoon

[백준] 2997 네 번째 수 - Implementation / Java

by graycode 2025. 7. 9.

 문제 링크

https://www.acmicpc.net/problem/2997

 

 풀이 코드

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int[] arr = new int[]{read(), read(), read()};
        Arrays.sort(arr);
        int a = arr[1] - arr[0], b = arr[2] - arr[1];

        bw.write(String.valueOf(a > b ? arr[0] + b : a < b ? arr[1] + a : arr[2] + a));
        bw.flush();
    }

    private static int read() throws IOException {
        int c, n = System.in.read() & 15;
        boolean flag = n == 13;

        if (flag) n = System.in.read() & 15;
        while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);

        return flag ? ~n + 1 : n;
    }

}

댓글