본문 바로가기

연습문제12

[프로그래머스][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++] 최솟값 만들기 #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++] 최댓값과 최솟값 #include #include #include using namespace std; string solution(string s) { string answer = ""; vector nums; for(int i = 0; i < s.size(); i++) { int addNum = 0; bool isMinus = false; while(s[i] != ' ' && i < s.size()) { int curNum = s[i] - '0'; // 음수인 경우 if(s[i] == '-') { isMinus = true; i++; continue; } addNum = addNum * 10 + curNum; i++; } if(isMinus) addNum *= -1; nums.push_back(addNum); } so.. 2023. 10. 30.
[프로그래머스][C++] 공원 산책 #include #include #include #include using namespace std; vector solution(vector park, vector routes) { vector answer; map obstacles; // 장애물의 좌표들 // 현재 위치 => {posX, posY} int posX, posY; // 시작 지점과 장애물 탐색 for(int i = 0; i < park.size(); i++) { for(int j = 0; j < park[i].size(); j++) { vector v = {i, j}; // 탐색할 위치 if(park[i][j] == 'S') { // 시작 위치 설정 posX = i; posY = j; } else if(park[i][j] == 'X') .. 2023. 10. 27.
[프로그래머스][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++] 달리기 경주 #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++] 하노이의 탑 #include #include using namespace std; vector hanoi(int curNum, int n, vector pred) { if(curNum == n) return pred; curNum++; vector curVec = pred; // 1. 이전 원판들을 2번으로 옮김(2와 3을 바꿈) for(int i = 0; i < pred.size(); i++) { if(pred[i][0] == 2) { pred[i][0] = 3; } else if(pred[i][0] == 3) { pred[i][0] = 2; } if(pred[i][1] == 2) { pred[i][1] = 3; } else if(pred[i][1] == 3) { pred[i][1] = 2; } } // 2. k번.. 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.
[프로그래머스][C++] 카드 뭉치 #include #include using namespace std; string solution(vector cards1, vector cards2, vector goal) { string answer = "Yes"; int x = 0; // card1에서 나와야하는 단어의 인덱스 번호 int y = 0; // card2에서 나와야하는 단어의 인덱스 번호 for(int i = 0 ; i < goal.size(); i++) { string curWord = goal[i]; if(curWord == cards1[x]) { // 현재 단어가 카드뭉치 1에 있는 경우 x++; } else if(curWord == cards2[y]) { // 현재 단어가 카드뭉치 2에 있는 경우 y++; } else { ans.. 2023. 9. 12.
[프로그래머스][C++] 추억 점수 #include #include #include using namespace std; vector solution(vector name, vector yearning, vector photo) { vector answer; map scores; for(int i = 0 ; i < name.size(); i++) { // 이름별 그리움 점수 저장 scores[name[i]] = yearning[i]; } for(int i = 0; i < photo.size(); i++) { answer.push_back({0}); for(int j = 0; j < photo[i].size(); j++) { string curName = photo[i][j]; answer[i] += scores[curName]; } } r.. 2023. 9. 12.