본문 바로가기

c++92

[C++] char형에서 int형으로 형 변환 오늘 오랜만에 프로그래머스 문제를 풀다가 char형을 int형으로 형 변환시키는 것에서 순간 막혔다. string형은 int형으로 바꾸려면 stoi를 사용하면 되는데 char형은 뭘로 바꿔야 하지? 하다가 stoi랑 같이 나오는 atoi가 const char * 를 인수로 받길래 이거구나 싶어서 해봤는데 잘 되지 않았다. 그래서 검색해서 찾아보았고, 알고보니 쉬웠다. char형은 아스키코드이기 때문에 현재 char에서 '0'의 아스키코드 값을 빼주어 구할 수 있었다. char c = '5'; int i = c - '0'; cout 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; 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(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 number, int n, int m) { int answer = 0; if(number % n == 0 && number % m == 0) return 1; return answer; } 1. %연산을 이용해 나머지가 0인 경우를 판별하여 배수인지를 확인한다. 2. number를 n으로 나눴을 때의 나머지가 0이면서 m으로 나눴을 때도 나머지가 0이라면 1을 리턴한다. 3. 그 외의 경우에는 0을 리턴한다. 잠깨기 용으로 쉽게 풀었다. 2023. 10. 12.
[프로그래머스][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.
[백준/BOJ][C++] 26069번 붙임성 좋은 총총이 #include #include using namespace std; int main() { // 쓰레드 환경이 아닐때 버퍼를 분리하여 처리속도를 빠르게 해줌 ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int N; cin >> N; map rainbowDancing; // 무지개 댄스를 추고있는 사람 rainbowDancing.insert({ "ChongChong", true }); // 총총이를 추가 int count = 1; for (int i = 0; i > a >> b; if ((!rainbowDancing[a] && !rainbowDancing[b]) || (rainbo.. 2023. 9. 5.