본문 바로가기

프로그래머스40

[프로그래머스][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++][2단계] 다음 큰 숫자 #include #include using namespace std; int solution(int n) { // 1. n을 2진수로 변환했을 때의 1의 갯수 구하기 // 2. n+1부터 1씩 증가시켜가면서 2진수로 변환했을 때의 1의 갯수 구하기 // 3. n과 1의 갯수 비교후 같으면 해당 수를 리턴 int nCount = 0; while(true) { int count = 0; // 현재 수를 이진수로 바꿨을 때 맨 뒷자리 수부터 1인지 확인 for(int i = 0; (n>>i) > 0; i++) { // 시프트 연산을 이용해 나온 수가 홀수인지 판단하여 마지막 자리의 수를 확인할 수 있음. if(((n>>i) % 2) == 1) count++; } if(nCount == 0) nCount = c.. 2023. 11. 9.
[프로그래머스][C++][2단계] 숫자의 표현 #include #include using namespace std; int solution(int n) { int answer = 1; // 자기자신은 세고 들어감 if(n != 1 && n % 2 != 0) answer++; for(int i = 3; i 2023. 11. 7.
[프로그래머스][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++][2단계] 이진 변환 반복하기 #include #include using namespace std; vector solution(string s) { vector answer; int num = 0; // 반복 횟수 int count = 0; // 제거된 0의 갯수 while(s != "1") { num++; // 모든 0 제거 string x; for(int i = 0; i 0) { bin += ((c % 2) + '0'); // 역순으로 저장됨 c /= 2; } // 역순 -> .. 2023. 11. 3.
[프로그래머스][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++][2단계] 올바른 괄호 #include #include using namespace std; bool solution(string s) { bool answer = true; int count = 0; for(int i = 0; i 0) answer = false; return answer; } 1. '('와 ')'의 갯수를 세서 같은 경우에만 올바른 것이므로 해당 경우를 찾아야한다. 2. 먼저 갯수를 셀 count 변수를 선언하고, 문자열 s를 반복하여 각 문자들을 탐색한다. 3. 문자가 '(' 인 경우는 count를 증.. 2023. 11. 1.
[프로그래머스][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++] 최솟값 만들기 #include #include #include using namespace std; int solution(vector A, vector B) { int answer = 0; sort(A.begin(), A.end()); sort(B.rbegin(), B.rend()); for(int i = 0; i < A.size(); i++) { answer += A[i] * B[i]; } return answer; } 1. 두 배열의 원소들을 각각 곱해서 더한 값이 최소가 나오려면, 한 쪽의 가장 작은 값과 다른 한 쪽의 가장 큰 값을 곱하고, 그 다음 작은 값과, 다른 한 쪽의 그 다음 큰 값을 곱하고 이렇게 한 쪽은 작은 값부터 오름차순으로 다른 한 쪽은 큰 값부터 내림차순으로 쌍을 지어 곱해주어야 한다. 2.. 2023. 10. 31.
[프로그래머스][C++] JadenCase 문자열 만들기 #include #include using namespace std; string solution(string s) { for(int i = 0; i = 'a' && s[i] 대문자로 변경 s[i] = s[i] - 'a' + 'A'; continue; } if((s[i] == ' ') && (s[i + 1] >= 'a' && s[i + 1] 대문자로 변경 s[i+1] = s[i+1] - 'a' + 'A'; continue; } if(i != 0 && s[i - 1] != ' ' && s[i] >= 'A' && s[i] 소문자로 변경 s[i] = s[i] - 'A' + 'a'; } } return s; } 1. 입력받은 문자열 s의 각 .. 2023. 10. 31.
[프로그래머스][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.