코테 좀 해라★彡9 짝지어 제거하기 #include #include using namespace std; int solution(string s) { int answer = -1; if(s.size() 코테 좀 해라★彡/C++ 2024. 4. 2. 피보나치 수 코드를 이렇게 짯는데 테스트 7부터 실패로 뜬다.. 해결 방법을 몰라서 질문하기 통해서 봤는데.. 애초에 n이 담길 수 있는 범위가 한정적이여서 오버플로우가 되면 이상한 값이 들어간다고한다. 따라서, 값을 넣어줄 때 부터 1234567의 나머지를 넣어주면 된다. #include #include #include using namespace std; int solution(int n) { int answer = 0; vector array; array.resize(n + 1); array[0] = 0; array[1] = 1; for(int i = 2; i 코테 좀 해라★彡/C++ 2024. 4. 2. 다음 큰 숫자 #include #include #include using namespace std; int solution(int n) { int answer = 0; //1.일단 n을 이진수로 변환시키기 string binary = ""; while(n != 0) { binary = to_string(n % 2) + binary; n /= 2; } //2. 뒤 인덱스부터 보고 자기가 1일때 앞 인덱스에 0이 있는지 확인 int idx = 0; for(int i = binary.size() - 1; i > 0; i--) { if(binary[i] == '1' && binary[i-1] == '0') { binary[i-1] = '1'; binary[i] = '0'; idx = i; break; } } //3. idx가.. 코테 좀 해라★彡/C++ 2024. 4. 1. 숫자의 표현 #include #include #include using namespace std; int solution(int n) { int answer = 0; //1부터 n까지 이중 for문으로 더해줘서 비교해줌 for (int i = 1; i 코테 좀 해라★彡/C++ 2024. 4. 1. 이진 변환 반복하기 #include #include #include using namespace std; vector solution(string s) { vector answer; //갯수를 셀 cnt int cnt = 0; int zeroCnt = 0; //"1"일때 까지 반복 while(s != "1") { cnt++; //몇번 반복했는지 세기 //1. 비교해서 0을 제거한다. string compareStr = ""; for(int i = 0 ; i < s.size(); i++) { if(s[i] == '1') compareStr += '1'; else zeroCnt++; //0이 나올때 마다 count } //2. 비교한 문자열의 길이를 이진수로 변환해준다 int num = compareStr.size(); str.. 코테 좀 해라★彡/C++ 2024. 3. 28. 올바른 괄호 #include #include #include // 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요. bool solution(const char* s) { //기본 true 값 bool answer = true; //count값 int countA = 0; int countB = 0; //문자열을 비교하고 카운트 한다 for(int i = 0; i countA) { answer = false; break; } } if(answer && countA == countB) answer = true.. 코테 좀 해라★彡/C++ 2024. 3. 28. 최솟값 만들기 #include #include #include //sort() 위해 추가 using namespace std; int solution(vector A, vector B) { int answer = 0; //a에서 오름차순을 해주고 b는 내림차순을 해줘서 각 같은 인덱스 끼리 곱해주고 더해주면 된다 //1. A 오름차순 sort( A.begin(), A.end() ); cout 코테 좀 해라★彡/C++ 2024. 3. 25. JadenCase 문자열 만들기 #include #include using namespace std; string solution(string s) { string answer = ""; //아스키 코드를 알아야한다 //소문자 > 대문자 > 숫자 //제대로 모를땐 빼주면 됨 int index = 'A'-'a'; for(int i = 0 ; i = 'a' && s[i] = 'a'&& s[i] = 'A' && s[i] 코테 좀 해라★彡/C++ 2024. 3. 25. 최댓값과 최솟값 #include #include using namespace std; string solution(string s) { string answer = ""; 1. 공백으로 나누기 vector result; size_t cutAt = 0; while ((cutAt = s.find_first_of(" ")) != s.npos) //공백으로 나누기 { if (cutAt > 0) result.push_back(s.substr(0, cutAt)); s = s.substr(cutAt + 1); } //마지막 단어 넣어주기 if (s.size() > 0) result.push_back(s); //2. min, max 구하기 int min = stoi(result[0]); int max = stoi(result[0]);.. 코테 좀 해라★彡/C++ 2024. 3. 25. 이전 1 다음