본문 바로가기

코딩테스트100

[프로그래머스][C++] 둘만의 암호 #include #include #include using namespace std; string solution(string s, string skip, int index) { string answer = ""; // 해당 단어를 만났을 때 스킵할지 여부 map IsSkip; for(int i = 0; i 'z') { .. 2023. 10. 19.
[프로그래머스][C++] 문자열 돌리기 #include #include using namespace std; int main(void) { string str; cin >> str; for(int i = 0; i < str.size(); i++) { cout 2023. 10. 19.
[프로그래머스][C++] 대충 만든 자판 #include #include #include using namespace std; vector solution(vector keymap, vector targets) { vector answer; map key; // 키맵을 돌면서 각 단어별 찾아야되는 횟수를 미리 파악 for(int i = 0; i 최소 횟수로 변경 if(key[curWord] ==.. 2023. 10. 18.
[프로그래머스][C++] 조건에 맞게 수열 변환하기 2 #include #include #include using namespace std; int solution(vector arr) { int x = 0; while(true) { vector prevArr = arr; // 이전 배열 for(int i = 0; i = 50 && arr[i] % 2 == 0) { // 50보다 크거나 같고, 짝수인 경우 arr[i] /= 2; } else if(arr[i] < 50 && arr[i] % 2 == 1) { // 50보다 작고, 홀수인 경우 arr[i] = arr[i] * 2 + 1; } } // 정렬 sort(prevArr.begin(), prevArr.end()); sort(arr.begin(), a.. 2023. 10. 18.
[프로그래머스][C++] 달리기 경주 #include #include #include using namespace std; vector solution(vector players, vector callings) { map playersRank; for(int i = 0; i < players.size(); i++) { playersRank[players[i]] = i; } for(int i = 0; i < callings.size(); i++) { // 현재 플레이어 등수 int curPlayerRank = playersRank[callings[i]]; playersRank[players[curPlayerRank]]--; // 현재 플레이어 순위 상승 playersRank[players[curPlayerRank - 1]]++; // 추월당한.. 2023. 10. 17.
[프로그래머스][C++] 뒤에서 5등까지 #include #include #include using namespace std; vector solution(vector num_list) { vector answer; for(int i = 0; i num_list[j]) { curMin = num_list[j]; minIndex = j; } } answer.push_back(curMin); swap(num_list[i], num_list[minIndex]); } return answer; } 1. 가장 작은 5개의 수를 구해야 하므로 .. 2023. 10. 17.
[프로그래머스][C++] 숫자 짝꿍 #include #include using namespace std; string solution(string X, string Y) { string answer = ""; // 0~9 까지의 갯수를 담을 벡터 vector Xcount(10); vector Ycount(10); // X의 각 숫자들의 갯수를 셈 for(int i = 0; i int로 변환 Xcount[lastNum]++; } // Y의 각 숫자들의 갯수를 셈 for(int i = 0; i .. 2023. 10. 17.
[프로그래머스][C++] 조건에 맞게 수열 변환하기 3 #include #include using namespace std; vector solution(vector arr, int k) { if(k % 2 == 0) { // k가 짝수 for(int i = 0; i < arr.size(); i++) { arr[i] += k; } } else { // k가 홀수 for(int i = 0; i < arr.size(); i++) { arr[i] *= k; } } return arr; } 1. k를 2로 나눈 나머지 값을 이용해 짝수인지 홀수인지 판단한다. 2. 짝수라면 주어진 arr를 반복하며 각 원소에 k만큼 더해준다. 3. 홀수라면 주어진 arr를 반복하며 각 원소에 k만큼 곱해준다. 4. arr를 리턴해준다. 어렵지 않게 금방 풀었다. 2023. 10. 16.
[프로그래머스][C++] 키패드 누르기 #include #include using namespace std; string solution(vector numbers, string hand) { string answer = ""; // 각 손의 초기 위치 int curLeft = 10; // * int curRight = 12; // # for(int i = 0; i < numbers.size(); i++) { // 0은 위치상 11로 간주 int curNum = numbers[i] == 0 ? 11 : numbers[i]; if(curNum % 3 == 1) { // 1, 4, 7 answer += "L"; curLeft = curNum; } else if(curNum % 3 == 0) { // 3, 6, 9 answer += "R"; cur.. 2023. 10. 16.
[프로그래머스][C++] 공백으로 구분하기 1 #include #include using namespace std; vector solution(string my_string) { vector answer; string curStr = ""; for(int i = 0 ; i < my_string.size(); i++) { if(my_string[i] == ' ') { answer.push_back(curStr); curStr = ""; } else { curStr += my_string[i]; } } answer.push_back(curStr); return answer; } 1. 각 단어들을 담을 curStr 선언 2. 문자열을 반복하면서 공백이 나오는지 체크 3. 공백이 나오지 않았다면 curStr에 현재 문자를 추가 4. 공백이 나왔다면 지금껏.. 2023. 10. 16.
[프로그래머스][C++] 접미사인지 확인하기 #include #include using namespace std; int solution(string my_string, string is_suffix) { int answer = 1; for(int i = 0; i < is_suffix.size() ; i++) { if(my_string[my_string.size() - 1 - i] != is_suffix[is_suffix.size() - 1 - i]) return 0; } return answer; } 1. 0부터 is_suffix의 크기만큼 반복을 진행 2. 문자열 my_string와 is_suffix의 마지막원소부터 하나씩 앞으로 나가면서 비교를 진행 3. 비교하는 중에 하나라도 틀리다면 0을 리턴 4. 반복문을 빠져나왔다면 is_suffix의.. 2023. 10. 13.
[프로그래머스][C++] 덧칠하기 #include #include using namespace std; int solution(int n, int m, vector section) { int answer = 0; int index = 0; for(int i = section[index++]; i 리턴 if(index >= section.size()) return answer; } i = section[index]; // 현재 섹션을 반복자로 재정의 } return answer; } 1. answer과 index를 선언 2. 반복자 i가 섹션의 0번째 인덱스부터 시작해 n이 될 때까지 반복을 진행한다. 3. 매 반복은 페인트칠을 하는 횟수를 의미하므로 answer을 1씩 증가시킨다. 4. 이후 섹션의 다음 수가 현재 수에 롤러의 길이를 더.. 2023. 9. 13.