-
[c++][구현] 백준 20053번: 최소, 최대 2알고리즘 2022. 1. 5. 16:36
문제 풀이
1. 배열을 사용하여 해결
2. 배열을 사용하지 않고 해결
코드
1. 배열에 저장하여 정렬을 사용하여 해결
#include <iostream> #include <algorithm> using namespace std; int main() { cout.tie(NULL); cin.tie(NULL); ios::sync_with_stdio(false); int test; cin >> test; for (int t = 0; t < test; t++) { int n; int m[1000000] = { 0 }; cin >> n; for (int i = 0; i < n; i++) { cin >> m[i]; } sort(m, m + n); cout << m[0] << " " << m[n - 1] << "\n"; } return 0; }
2. 배열을 사용하지 않고 해결
#include <iostream> using namespace std; int main() { cout.tie(NULL); cin.tie(NULL); ios::sync_with_stdio(false); int test; cin >> test; for (int t = 0; t < test; t++) { int min = 1000000; int max = -1000000; int n; cin >> n; for (int i = 0; i < n; i++) { int m; cin >> m; if (min > m) min = m; if (max < m) max = m; } cout << min << " " << max << "\n"; } return 0; }
성능 비교
1번 코드 2번 코드 성능을 비교했을때 배열을 사용하지 않는 것이 시간이 더 빠르다.
'알고리즘' 카테고리의 다른 글
[c++][완전탐색][브루트 포스] 백준 2798번: 블랙잭 (0) 2022.01.14 [c++][그래프] 백준 1260번: DFS와 BFS (0) 2022.01.13 [c++][구현] 백준 5597번: 과제 안 내신 분..? (0) 2022.01.11 [c++][자료구조][큐] 백준 2164번: 카드2 (0) 2022.01.10 [c++][시뮬레이션] 백준 20436번: ZOAC 3 (0) 2022.01.04