Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Archives
Today
Total
관리 메뉴

Life Engineering

[프로그래머스] 신규 아이디 추천 (C++) 본문

Problem Solving

[프로그래머스] 신규 아이디 추천 (C++)

흑개 2021. 9. 16. 17:08

https://programmers.co.kr/learn/courses/30/lessons/72410

 

코딩테스트 연습 - 신규 아이디 추천

카카오에 입사한 신입 개발자 네오는 "카카오계정개발팀"에 배치되어, 카카오 서비스에 가입하는 유저들의 아이디를 생성하는 업무를 담당하게 되었습니다. "네오"에게 주어진 첫 업무는 새로

programmers.co.kr

 

#include <string>
#include <vector>
#include <cctype>

using namespace std;

string solution(string new_id) {        
    string answer = "";
    string answer1 = "";
    for (int i=0; i<new_id.size(); i++){    //1,2 단계
        if (('a'<=new_id[i] && 'z'>=new_id[i]) || ('0' <=new_id[i] && '9'>=new_id[i]) || 
           new_id[i]=='-' || new_id[i]=='_' || new_id[i]=='.'){      //정상적이면 넣어주기
            answer1+=new_id[i];
        }
        else if (isupper(new_id[i])){        //소문자 변환
            answer1+=tolower(new_id[i]);
        }
    }
    for (int i=0; i<answer1.size(); i++){        //3단계
        answer+=answer1[i];
        if (answer1[i]=='.'){
           while (1){
               if (i+1<=answer1.size()-1 && answer1[i+1]=='.'){     //. 2개 이상 있을 경우 넘기기
                    i++;
               }
               else{
                   break;
               }
           }
       }
    }
    if (answer[0]=='.'){
        answer.erase(answer.begin());
    }
    if (answer[answer.size()-1]=='.'){
        answer.erase(answer.end()-1);
    }
    if (answer.size()==0){
        answer+='a';
    }
    if (answer.size()>=16){
        answer=answer.substr(0,15);
        if (answer[answer.size()-1]=='.'){
            answer.erase(answer.end()-1);
        }
    }
    if (answer.size()<=2){
        while (1){
            answer+=answer.back();
            if (answer.size()==3){
                break;
            }
        }
    } 
    return answer;
}

 

간단한 구현문제이나.. 많이 버벅인 점..

단계를 구분해서 코딩해주는게 중요한듯.. 안그러면 꼬이는 부분이 좀 생긴다

 

연속되는 마침표를 치환해줄때 이런 식으로 해준다.

'.' 가 나타날 경우, 일단 그걸 넣어주고 -> i를 증가시켜 i의 범위가 문자열 사이즈를 넘어서지 않고 증가시킨 i 번째 인덱스의 문자가 '.' 이면 계속해서 증가시켜준다. 탈출조건은 i 범위 넘거나 . 가 아닐 경우이다.