ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 제1회 온코더 공식 코딩테스트
    기교 2018. 7. 28. 16:25
    반응형

    1번 문제.

    #include <string>
    using namespace std;
    
    //핵심 소스코드의 설명을 주석으로 작성하면 평가에 큰 도움이 됩니다.
    class Solution{
    public:
        string decryptSpell(string str){
            string r;
            const int size = str.size();
            //r.push_back(str[0]);
            for (int i=0; i<size; i++) {
        
                    // 알파벳인 경우 
                if (isalpha(str[i])) {
                    if ((i+1) % 3 == 0) {
                        if (str[i] >= 'A' && str[i] <= 'Z') {
                            r.push_back('!');
                        } else {
                            char sz = str[i] - 32;
                            r.push_back(sz);
                        }
                    } else {
                        r.push_back(str[i]);
                    }
        
                } else {
        
                    // 알파벳이 아닌 경우
                    if ((i+1) % 3 == 0) {
                        r.push_back('!');
                    }
                    else {
                        r.push_back(str[i]);
                    }
                }
            }
            return r;
        }
    };
    
     

    2번 문제.

    내공 딸려서 패스

     

    3번 문제.

    #include <string>
    #include <algorithm>
    using namespace std;
    
    //핵심 소스코드의 설명을 주석으로 작성하면 평가에 큰 도움이 됩니다.
    class Solution{
    public:
        void debugPrint(string s, int n) {
            const int size = s.size();
            for(int i=0; i<size; i++) {
                cout << s[i];
        
                if ((i+1)%n == 0 ) {
                    cout << endl;
                    continue;
                }
            }
        
            cout << endl;
        }
        int translate(char s) {
            if (s == ' ') {
                return 62;
            } else if (s >= '0' && s <= '9') {
                return 52 + s - '0';
            } else if (s >= 'a' && s <= 'z') {
                return s - 'a';
            } else {
                return 26 + s - 'A';
            }
        }
        
        void translate_to_6_digit(string &s, char n) {
            if (n == 0) {
                for(int i=0; i<6; i++) {
                    s.append("0");
                }
            }
            else {
                for (int i = 5; i >= 0; i--)  {
                    int cand = n >> i & 1;
                    if (cand == 1) {
                        s.append("1");
                    } else {
                        s.append("0");
                    }
                }
            }
        }
        
        string digit_to_hex(string s) {
            string r;
            char table[4] = {1,2,4,8};
        
            const int size = s.size();
            for (int i=0; i<size; i+=4) {
                string cand = s.substr(i, 4);
                reverse(cand.begin(), cand.end());
        
                int sum = 0;
                for (int j=0; j<4; j++) {
                    sum += (cand[j] - '0') * table[j];
                }
                //cout << sum << CENDL;
        
                if (sum > 9) {
                    sum = sum - 10;
                    r.push_back('A' + sum);
                } else {
                    r.append(to_string(sum));
                }
            }
        
            return r;
        }
        
        string encoder(string message){
            const int size = message.size();
            vector<char> arr;
            for (int i=0; i<size; i++) {
                char sz = message[i];
                char digit = translate(sz);
                arr.push_back(digit);
            }
            // 변환 -> 6 진수 
            const int size2 = arr.size();
            string s;
            for(int i=0; i<size2; i++) {
                translate_to_6_digit(s, arr[i]);
            }
            
            //debugPrint(s, 6);
            arr.clear();
        
            // 8로 나누어 떨어지지 않을 때 0 으로 채우기 
            const int size3 = s.size();
            if (size3 % 8 != 0) {
                int cand = (size3 - size3 % 8) / 8 + 1;
                cand = cand * 8 - size3;
                for (int i=0; i<cand; i++) {
                    s.append("0");
                }
            }
        
            //debugPrint(s, 8);
        
            string sol = digit_to_hex(s);
            return sol;
        }
    };
    
     

    4번 문제.

    내공 딸려서 패스

    반응형

    댓글

Designed by Tistory.