코딩테스트 128
-
문제 풀이#include #include using namespace std;int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); queue q; string command = ""; int n = 0, input = 0; cin >> n; for (int i = 0; i > command; if (command == "push") { cin >> input; q.push(input); } else if (command == "pop") { if (q.empty()) { cout 메모fr..
-
-
문제 풀이#include using namespace std;int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n = 0, k = 0, temp = 0; int result = -100000; int psum[100001] = { 0 }; cin >> n >> k; for (int i = 1; i > temp; psum[i] = psum[i - 1] + temp; } for (int i = k; i 메모지난 문제에 이어서 구간합을 사용해서 풀이한 문제이다.이때 기존에 저장된 result값과 현재 구해진 구간합의 값을 비교해 더 큰 값이 result에 할당되도록 max 함수를 사용했다.
-
-
문제 풀이#include #include #include using namespace std;int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n = 0, m = 0; cin >> n >> m; vectorinput(n); vectorresult; for (int i = 0; i > input[i]; } sort(input.begin(), input.end()); string target = ""; int count = 0; for (int i = 0; i > target; if (binary_search(input.begin(), input.end(), target)) { count++; } } cout 메모제한..
-
문제 풀이#include #include #include using namespace std;int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n = 0, value = 0; vector v; cin >> n; for (int i = 0; i > value; v.push_back(value); } // 이진 탐색에 들어가기 전에 먼저 벡터 안의 데이터를 sort 해준다. sort(v.begin(), v.end()); cin >> n; // 이진 탐색 함수를 사용해 벡터 내부에 입력받은 값이 존재하는지 확인한다. fo..
-
문제 풀이#include #include using namespace std;int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int input[9] = { 0 }; for (int i = 0; i > input[i]; } sort(input, input + 9); do { int sum = 0; for (int i = 0; i 메모문제의 기본 원리는 9개의 수 중에서 7개의 수를 조합해 그 합이 100이 되는 경우 해당값들을 출력하는 것이다.DFS 등으로 구하는 방법도 있지만 값을 빠르게 찾기 위해서 순열과 조합을 구할 때 유용한 algrorithm 헤더에 있는 next_permutaion() 함수를 사용했다...
-
문제 풀이vector에 값을 입력받아서 바로 sort 함수로 푸는 방법이 가장 간단하지만, 단계별로 풀어보기에 제시된 문제 설명에 따르면 이 문제의 원래 의도는 시간 복잡도가 O(n²)인 정렬 알고리즘 즉, 2중 for문을 사용하는 정렬을 사용해보는 것이다.해당 시간복잡도에 해당하는 정렬의 예로 삽입 정렬(시간복잡도상 최고는 O(n), 최악과 평균은 O(n²)), 거품 정렬(=버블 정렬), 선택 정렬이 있다.따라서 이번 문제는 내장 정렬함수, 삽입정렬, 버블정렬, 선택정렬 이렇게 4가지 경우를 모두 풀어보았다.1. sort함수 사용#include #include #include using namespace std;int main(){ vector v; int n = 0; cin >> n; for (int..