1 분 소요

Question

Q Q2


Solution

  • answer.siz()가 t가 될때까지 while 루프를 돈다.
  • n진수 변환을 하되, 10 ~ 15를 A ~ F 로 바꿔서 변환시킨다.
    변환된 string n진수에서 뒤에서 부터 꺼낼 것이기 때문에 변환 마지막에 reverse는 안 해줘도 된다.
  • 내 턴인지 판별하고 맞다면 answer에 해당 문자를 넣어준다.

Cord

#include <string>
#include <vector>

using namespace std;

int myTurn(int cnt, int m)
{
    return cnt % m;
}

string solution(int n, int t, int m, int p)
{
    string answer = "";

    int num = 0;
    int cnt = 0;

    while (answer.size() < t)
    {
        int temp = num;

        // n진수 변환
        string strNum = "";
        if (temp == 0) { strNum = "0"; }
        while (temp != 0)
        {
            int cal = temp % n;
            char res;
            if (cal == 10) { res = 'A'; }
            else if (cal == 11) { res = 'B'; }
            else if (cal == 12) { res = 'C'; }
            else if (cal == 13) { res = 'D'; }
            else if (cal == 14) { res = 'E'; }
            else if (cal == 15) { res = 'F'; }
            else { res = cal + '0'; }

            strNum += res;
            temp /= n;
        }
        
        // 문자 하나씩 빼기
        while (strNum.size() > 0)
        { 
            // 내 턴인 경우 answer에 넣기
            if (myTurn(cnt, m) == p-1) 
            { 
                answer.push_back(strNum.back());
                
                // t만큼 채워지면 종료
                if (answer.size() >= t) { return answer; }
            }

            strNum.pop_back();
            ++cnt;
        }

        ++num;
    }

    return answer;
}

Result

Result