🔻PS/Baekjoon

[Baekjoon] 백준 20053 최소, 최대 2 C++

_니지 2022. 9. 28. 10:50

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

 

20053번: 최소, 최대 2

N개의 정수가 주어진다. 이때, 최솟값과 최댓값을 구하는 프로그램을 작성하시오.

www.acmicpc.net

 

문제

N개의 정수가 주어진다. 이때, 최솟값과 최댓값을 구하는 프로그램을 작성하시오.

입력

첫째 줄에 테스트 케이스의 개수 T (1 ≤ T ≤ 10)가 주어진다. 각 테스트 케이스는 두 줄로 이루어져 있다.

각 테스트 케이스의 첫째 줄에 정수의 개수 N (1 ≤ N ≤ 1,000,000)이 주어진다. 둘째 줄에는 N개의 정수를 공백으로 구분해서 주어진다. 모든 정수는 -1,000,000보다 크거나 같고, 1,000,000보다 작거나 같은 정수이다.

출력

각 테스트 케이스마다 주어진 정수 N개의 최솟값과 최댓값을 공백으로 구분해 한 줄에 하나씩 차례대로 출력한다.

예제 입력 1 

3
5
20 28 22 25 21
5
30 21 17 25 29
5
20 10 35 30 7

예제 출력 1 

20 28
17 30
7 35

 

코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using namespace std;
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <functional>
#include <cmath>
#include <map>
#include <set>
#define _CRT_SECURE_NO_WARNINGS
#define INF 987654321
#define ll long long
typedef pair<intint> PII;
 
 
 
int main() {
    //없으면 시간초과 발생
    ios::sync_with_stdio(false);
    cin.tie(NULL);
 
    int t;
    cin >> t;
 
    while (t--) {
        int maxVal = -1000000;
        int minVal = 1000000;
        int n;
        cin >> n;
 
        while (n--) {
            int num;
            cin >> num;
 
            if (num > maxVal) maxVal = num;
            if (num < minVal) minVal = num;
        
        }
 
        cout << minVal << " " << maxVal << "\n";
    
    }
 
 
 
    return 0;
}
cs

 

728x90
반응형