Updated:

1. 문제 링크

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

2. 사용 알고리즘

구현

3. 풀이

1 ~ N까지 반복문을 돌며 값을 비교하여 최대값, 최소값 계산

4. 소스 코드

4-1. C++

https://github.com/dev-aiden/problem-solving/blob/main/boj/10818.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int main(void) {
    ios_base::sync_with_stdio(false);
    int n; cin >> n;
    int min = 1000003, max = -1000003;
    for (int i = 0; i < n; ++i) {
        int num; cin >> num;
        if (num < min) min = num;
        if (num > max) max = num;
    }
    cout << min << ' ' << max << '\n';
    return 0;
}

4-2. JAVA

https://github.com/dev-aiden/problem-solving/blob/main/boj/10818.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int min = 1000003, max = -1000003;
        StringTokenizer st = new StringTokenizer(br.readLine());
        for(int i = 0; i < n; ++i) {
            int num = Integer.parseInt(st.nextToken());
            if(num < min) min = num;
            if(num > max) max = num;
        }
        System.out.println(min + " " + max);
    }
}

Updated:

Leave a comment