C++
-
C++ map STL 사용법유용한 정보 2021. 12. 5. 01:37
Map - NODE로 이루어져 있으며 KEY와 VALUE로 이루어진 트리입니다. -중복을 허용하지 않습니다. -기본 형태 map mapEx; -map은 자료 삽입시 자동으로 내부에서 정렬 기능을 제공합니다. 이때, KEY값을 기준으로 오름차순으로 정렬하게 됩니다. -내림차순으로 사용하고 싶은 경우 greater 인자를 추가하여 표현하면 됩니다. MAP 객체 선언 및 삽입 #include #include #include using namespace std; int main() { map mapEx; // string형도 KEY값이 가능 mapEx.insert(make_pair("HOO3", 4)); mapEx.insert(make_pair("HOO", 1)); mapEx.insert(make_pair("H..
-
주식가격프로그래머스(Programmers) 2021. 12. 3. 01:17
#include #include #include using namespace std; vector solution(vector prices) { int size=prices.size(); stack can; //stack에는 해당prices의 값이 들어가는 시간이 쌓인다. vector arr(size); //몇초 지났는지 넣을 vector for(int i=0; i prices[i]) // stack이 비어있지 않고 && 스택 맨위의 인덱스에 해당하는 prices의 가격 이 곧 들어올 prices보다 큰 경우 = 가격이 하락한 경우 { arr[can.top()] = i-can.top(); can.pop(); } can.push(i);// 하락한 경우가 아닐 경우, stack에 prices의 INDEX값 ..
-
프린터프로그래머스(Programmers) 2021. 11. 28. 21:04
#include #include #include #include #include #include using namespace std; int solution(vector priorities, int location) { int answer = 1; queue arr; //first : priority, second : index priority_queue priQue; for(int i=0; i queue와 priority queue를 사용하는 문제입니다. => priority를 사용하지 않을 경우 매번 지금 queue에 남아 있는 원소들 중 priority가 가장 높은 아이가 누구인지를 for문을 통해 찾아야 합니다. => 저는 이런방식을 사용하려 했지만, 오류가 나였으며 우선순위 큐를 사용할 경우 삽..
-
가장 큰 수프로그래머스(Programmers) 2021. 11. 26. 21:43
#include #include #include #include #include using namespace std; bool cmp(string A, string B) { return A+B > B+A; } string solution(vector numbers) { string answer = ""; vector arr; for(int i=0; i string형으로 변환 후, 해당 배열을 큰 순으로 정렬하는 과정이 필요합니다. => cmp 함수를 생성해 해당 배열을 sorting합니다. *** string형태의 숫자이지만 '1'뒤에 '2'가, '2'뒤에 '3'이 있으므로 cmp함수로 비교를 하여도 숫자형의 비교와 다르지 않습니다. => 정렬된 배열은 이미 각 원소중 가장 큰 부분의 수로 정렬되어 있는..
-
K번째 수프로그래머스(Programmers) 2021. 11. 25. 20:31
입출력 예 설명 [1, 5, 2, 6, 3, 7, 4]를 2번째부터 5번째까지 자른 후 정렬합니다. [2, 3, 5, 6]의 세 번째 숫자는 5입니다. [1, 5, 2, 6, 3, 7, 4]를 4번째부터 4번째까지 자른 후 정렬합니다. [6]의 첫 번째 숫자는 6입니다. [1, 5, 2, 6, 3, 7, 4]를 1번째부터 7번째까지 자릅니다. [1, 2, 3, 4, 5, 6, 7]의 세 번째 숫자는 3입니다. #include #include #include #include using namespace std; vector solution(vector array, vector commands) { vector answer; int comLimit = commands.size(); for(int i=0; i..
-
단어변환프로그래머스(Programmers) 2021. 11. 15. 23:28
입출력 예 begin target words return "hit" "cog" ["hot", "dot", "dog", "lot", "log", "cog"] 4 "hit" "cog" ["hot", "dot", "dog", "lot", "log"] 0 입출력 예 설명 예제 #1 문제에 나온 예와 같습니다. 예제 #2 target인 "cog"는 words 안에 없기 때문에 변환할 수 없습니다. #include #include #include #include using namespace std; vector wordCheck(51,false); vector wordsBook; int answer =100000; string targetWord; bool compare(string begin, string wor..
-
카펫프로그래머스(Programmers) 2021. 11. 13. 01:54
제한사항 갈색 격자의 수 brown은 8 이상 5,000 이하인 자연수입니다. 노란색 격자의 수 yellow는 1 이상 2,000,000 이하인 자연수입니다. 카펫의 가로 길이는 세로 길이와 같거나, 세로 길이보다 깁니다. 입출력 예 brown yellow return 10 2 [4, 3] 8 1 [3, 3] 24 24 [8, 6] #include #include using namespace std; vector solution(int brown, int yellow) { vector answer; int area = brown + yellow; for(int width =1; width (WIDTH-2) * (HEIGHT-2) = yellow라는 제한사항을 넣어주어야하는 문제입니다. => 해당문제에는 ..