본문 바로가기

0단계16

[프로그래머스][C++][0단계] 문자열 묶기 #include #include #include using namespace std; int solution(vector strArr) { int answer = 0; vector count(31); for(int i = 0; i < strArr.size(); i++) { string curStr = strArr[i]; count[curStr.size()]++; } sort(count.rbegin(), count.rend()); answer = count[0]; return answer; } 1. 각 원소의 길이별 갯수를 담아놓을 벡터 count를 선언. 2. 원소의 최대 길이는 30이므로 벡터의 인덱스가 0부터 30까지 있어야 하기에 count의 크기는 31로 초기화 3. 주어진 문자배열 strArr를.. 2023. 11. 13.
[프로그래머스][C++][0단계] 리스트 자르기 #include #include using namespace std; vector solution(int n, vector slicer, vector num_list) { vector answer; int a = slicer[0]; int b = slicer[1]; int c = slicer[2]; switch(n) { case 1: for(int i = 0; i 2023. 11. 7.
[프로그래머스][C++][0단계] A 강조하기 #include #include using namespace std; string solution(string myString) { string answer = ""; for(int i = 0; i 'A' && myString[i] 2023. 11. 3.
[프로그래머스][C++][0단계] 왼쪽 오른쪽 #include #include using namespace std; vector solution(vector str_list) { vector answer; for(int i = 0; i < str_list.size(); i++) { if(str_list[i] == "l") { for(int j = 0; j < i; j++) { answer.push_back(str_list[j]); } break; } if(str_list[i] == "r") { for(int j = i + 1; j < str_list.size(); j++) { answer.push_back(str_list[j]); } break; } } return answer; } 1. 주어진 벡터 str_list를 첫 원소부터 반복하면서 "l" .. 2023. 11. 2.
[프로그래머스][C++][0단계] 정수 부분 #include #include using namespace std; int solution(double flo) { int answer = flo / 1; return answer; } 1. 임의의 실수를 1로 나눴을 때 소수부분은 날아가고 정수부분만 남는 것을 이용하였다. 2. 주어진 실수 flo를 1로 나눈 몫을 answer에 저장한 뒤 리턴한다. 나눗셈의 몫을 이용하여 쉽게 풀었다. 2023. 11. 1.
[프로그래머스][C++] n 번째 원소까지 #include #include using namespace std; vector solution(vector num_list, int n) { vector answer; for(int i = 0; i < n; i++) { answer.push_back(num_list[i]); } return answer; } 1. 0부터 주어진 수 n까지 반복하며 answer에 num_list의 원소들을 넣어준다. 2. answer을 리턴한다. 이 문제를 몰라서 이 글을 볼 사람은 거의 없을 것 같다.ㅋㅋ 그 정도로 매우 쉬웠다! 2023. 10. 31.
[프로그래머스][C++] 할 일 목록 1 #include #include using namespace std; vector solution(vector todo_list, vector finished) { vector answer; for(int i = 0; i < todo_list.size(); i++) { if(!finished[i]) { // 일을 못 마침 answer.push_back(todo_list[i]); } } return answer; } 1. finished와 todo_list에서 같은 인덱스를 사용하므로 어느 한쪽의 크기를 받아 반복문을 진행하며 원소에 접근한다. 2. todo_list의 각 값을 finished를 이용해 체크하고, 일을 마치지 못 한 경우 라면 answer에 todo_list의 해당 원소를 넣어줌. 3... 2023. 10. 30.
[프로그래머스][C++] 마지막 두 원소 #include #include using namespace std; vector solution(vector num_list) { vector answer = num_list; int lastIndex = num_list.size() - 1; // 마지막 인덱스 int addNum; // 추가할 숫자 if(num_list[lastIndex] > num_list[lastIndex - 1]) { // 마지막 원소 > 마지막 직전 원소 addNum = num_list[lastIndex] - num_list[lastIndex - 1]; } else { // 마지막 원소 2023. 10. 27.
[프로그래머스][C++] 두 수의 연산값 비교하기 #include #include using namespace std; int solution(int a, int b) { int answer = 0; int i = b; int j = 2 * a * b; while(b > 0) { // b의 자릿수에 따라 a에 10을 곱하는 횟수가 정해짐 b /= 10; a *= 10; } i += a; // 최종 i = [a x {10^(b의 자릿수)}] + b answer = i >= j ? i : j; return answer; } #include #include #include using namespace std; int solution(int a, int b) { int answer = 0; int i = b + (a * pow(10, to_string(b).s.. 2023. 10. 25.
[프로그래머스][C++] 수 조작하기 2 #include #include using namespace std; string solution(vector numLog) { string answer = ""; for(int i = 1; i < numLog.size(); i++) { int curDiff = numLog[i] - numLog[i - 1]; if(curDiff == 1) { answer += "w"; } else if(curDiff == -1) { answer += "s"; } else if(curDiff == 10) { answer += "d"; } else if(curDiff == -10) { answer += "a"; } } return answer; } 1. numLog의 원소들의 각 차이를 이용하여 어떤 조작이 있었는지 알 수.. 2023. 10. 24.
[프로그래머스][C++] 신규 아이디 추천 #include #include using namespace std; string solution(string new_id) { for(int i = 0; i = '0' && new_id[i] = 'a' && new_id[i] = 'A' && new_id[i] 2023. 10. 20.
[프로그래머스][C++] 배열 만들기 3 #include #include using namespace std; vector solution(vector arr, vector intervals) { vector answer; int first_begin = intervals[0][0]; int first_end = intervals[0][1]; int second_begin = intervals[1][0]; int second_end = intervals[1][1]; for(int i = first_begin; i 2023. 10. 20.