Array: 백준 10818번 최소, 최대

jhkim·2023년 12월 21일

자료구조

목록 보기
6/7

Array를 활용하여 최소, 최대 값을 출력하는 문제이다.

import java.io.IOException;
import java.util.Scanner;

public class Q3 {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        int[] seq = new int[n];

        for(int i = 0; i < n; i++) {
            seq[i] = sc.nextInt();
        }

        int maxInt = Integer.MIN_VALUE;
        int minInt = Integer.MAX_VALUE;

        for (int item: seq){
            if (item >= maxInt){
                maxInt = item;
            } if (item <= minInt){
                minInt = item;
            }
        }

        System.out.print(minInt +" "+ maxInt);

    }
}

별도의 코멘트는 없으나, maxInt, minInt가 처음부터 오류없이 들어가게 하려면 MIN_VALUE, MAX_VALUE 등을 활용해야 했다. 숫자의 범위 등에 대해서 다시 체크할 수 있는 좋은 기회였다!

profile
다시 시작합니다 :)

0개의 댓글