728x90

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
#define N 5 | |
using namespace std; | |
int main() { | |
ios::sync_with_stdio(false), cin.tie(nullptr); | |
int a[N] = {}, sum = 0; | |
for (int i = 0; i < N; i++) cin >> a[i], sum += a[i]; | |
sort(a, a + N); | |
cout << sum / N << "\n"; | |
cout << a[2]; | |
} | |
/* | |
대표값2(2587) | |
O(n log n)? | |
입력받음과 동시에 sum에 저장하고, a배열을 sort() | |
평균은 sum/N, 중앙값은 a[2]를 출력 | |
*/ | |
/* 2. selection sort를 이용한 풀이 O(N^2)*/ | |
/* | |
#include <bits/stdc++.h> | |
#define N 5 | |
using namespace std; | |
int main() { | |
ios::sync_with_stdio(false), cin.tie(nullptr); | |
int a[N] = {}, sum = 0, m = 0; | |
for (int i = 0; i < N; i++) cin >> a[i], sum += a[i]; | |
for (int i = 0; i < N; i++) { | |
int indexOfMin = i; | |
for (int j = i + 1; j < N; j++) { | |
if (a[indexOfMin] > a[j]) { | |
indexOfMin = j; | |
} | |
} | |
if (indexOfMin != i) { | |
int lesser = a[indexOfMin]; | |
a[indexOfMin] = a[i]; | |
a[i] = lesser; | |
} | |
} | |
cout << sum / N << "\n"; | |
cout << a[2]; | |
} | |
*/ | |
// #include <bits/stdc++.h> | |
// using namespace std; | |
// int arr[5], sum; | |
// int main(void) { | |
// ios::sync_with_stdio(false), cin.tie(nullptr); | |
// for (int i = 0; i < 5; i++) cin >> arr[i]; | |
// for (int i = 0; i < 5; i++) sum += arr[i]; | |
// cout << sum / 5 << '\n'; | |
// sort(arr, arr + 5); | |
// cout << arr[2]; | |
// } |
'Algorithm > C&C++' 카테고리의 다른 글
백준 10093번: 숫자(c/c++, c/cpp) (0) | 2021.10.26 |
---|---|
백준 2309번: 일곱 난쟁이(c/c++, c/cpp) (0) | 2021.10.06 |
백준 2576번: 홀수(c/c++, c/cpp) (0) | 2021.10.05 |
백준 2562번: 최댓값(c/c++, c/cpp) (0) | 2021.10.05 |
백준 2490번: 윷놀이(c/c++, c/cpp) (0) | 2021.10.05 |