반응형
2750번 문제 : 수 정렬하기
https://www.acmicpc.net/problem/2750
2751번 문제 : 수 정렬하기 2
https://www.acmicpc.net/problem/2751
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 | #include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { int num; cin >> num; vector<int> v; while (num > 0) { int inp; cin >> inp; v.push_back(inp); num--; } sort(v.begin(), v.end()); for (auto i : v) { cout << i << "\n"; } return 0; } | cs |
2108번 문제 : 통계학
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | #include <iostream> #include <vector> #include <cmath> #include <algorithm> using namespace std; vector<int> v; int arr[8001] = {0}; int GetMean(); int GetMedian(); int GetMode(); int GetRange(); int main() { int inp; cin >> inp; while (inp > 0) { int num; cin >> num; v.push_back(num); inp--; } cout << GetMean() << endl; cout << GetMedian() << endl; cout << GetMode() << endl; cout << GetRange() << endl; return 0; } int GetMean() { int sum = 0; for (auto i : v) sum += i; double result = (double)sum / (double)v.size(); return round(result); } int GetMedian() { sort(v.begin(), v.end()); int medianIndex = v.size() / 2; //v size is always odd number according to the question return v.at(medianIndex); } int GetMode() { for (auto i : v) { if (i >= 0) arr[i]++; else arr[abs(i) + 4000]++; } int max = 0; int maxIndex = 0; bool isTheOnlyMax = false; for (int i = 0; i < sizeof(arr)/sizeof(*arr); i++) // sizeof(arr)/sizeof(*arr) = 8001 { if (arr[i] == max) isTheOnlyMax = false; if (arr[i] > max) { max = arr[i]; maxIndex = i; isTheOnlyMax = true; } } if (isTheOnlyMax) { int originalIndex; if (maxIndex > 4000) originalIndex = (maxIndex - 4000) * (-1); else originalIndex = maxIndex; return originalIndex; } else { vector<int> tempV; for (int i = 0; i < sizeof(arr) / sizeof(*arr); i++) // sizeof(arr)/sizeof(*arr) = 8001 { if (arr[i] == max) { if(i < 4000) tempV.push_back(i); else { int value = (i - 4000) * (-1); tempV.push_back(value); } } } sort(tempV.begin(), tempV.end()); if (tempV.size() > 1) //TODO is this needed? return tempV[1]; else return tempV[0]; } } int GetRange() { sort(v.begin(), v.end()); return v.back() - v.front(); } | cs |
1427번 문제 : 소트인사이드
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 | #include <iostream> #include <algorithm> #include <vector> #include <functional> using namespace std; int main() { int num; cin >> num; vector<int> v; while (num > 0) { int leftover = num % 10; v.push_back(leftover); num /= 10; } sort(v.begin(), v.end(), greater<int>()); for (auto i : v) { cout << i; } return 0; } | cs |
메모:
-greater<int>() 내림차순으로 정렬, #include <functional> 헤더파일 필요
1181번 문제 : 단어정렬
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 | #include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; bool Compare(const string& a, const string&b) { //CompareStringAlphabetically if (a.size() == b.size()) { int i = 0; while (a[i] == b[i]) { i++; if (i >= a.size()) break; } return(a[i] < b[i]); } //CompareStringByLength else return(a.size() < b.size()); } int main() { int num; cin >> num; vector<string> v; while (num > 0) { string inp; cin >> inp; v.push_back(inp); num--; } sort(v.begin(), v.end(), Compare); //vector sorting v.erase(unique(v.begin(), v.end()), v.end()); //erase duplication for (auto i : v) { cout << i << "\n"; } return 0; } | cs |
메모:
-vector의 중복제거 함수는 unique사용
-unigue후에 뒷부분을 erase처리해야함
반응형
'C++ Algorithm > 백준 알고리즘 문제 풀이' 카테고리의 다른 글
스택 사용하기 (기초) (0) | 2019.02.14 |
---|---|
소수 구하기 (0) | 2019.02.13 |
규칙 찾기 (0) | 2019.02.05 |
문자열 사용하기 3 (0) | 2019.02.05 |
문자열 사용하기 2 (0) | 2019.02.03 |