프로그래머스 봉인된 주문서
#pragma once
#include <stdio.h>
#include <string>
class C_SEAL
{
private:
int m_nPivot;
std::string m_strResult;
private:
void make(int &nIndex, int nTotal, int nDepth);
int findIndex(const char* p);
public:
C_SEAL() = default;
void init();
void print(int nIndex);
void toInt(const char* str);
};
#include "seal.h"
void C_SEAL::make(int& nIndex, int nTotal, int nDepth)
{
if (nIndex <= 0 || nIndex <= nTotal)
return;
make(nIndex, nTotal + nDepth, nDepth * m_nPivot);
int nPostion = (nIndex - nTotal - 1) % m_nPivot;
nIndex = (nIndex - 1) / m_nPivot;
m_strResult = (char)(nPostion + 97) + m_strResult;
}
int C_SEAL::findIndex(const char *p)
{
int nLength = strlen(p);
int nResult{};
for (int i = 0; i < nLength; i++)
{
nResult = (nResult + (p[i] - 97) + 1) * m_nPivot;
}
nResult /= m_nPivot;
return nResult;
}
void C_SEAL::init()
{
m_nPivot = 26;
}
void C_SEAL::print(int nIndex)
{
m_strResult.clear();
make(nIndex, 0, m_nPivot);
printf("%s\n", m_strResult.c_str());
}
void C_SEAL::toInt(const char* str)
{
printf("%d\n", findIndex(str));
}
#include <iostream>
#include "seal.h"
int main()
{
C_SEAL cSeal{};
cSeal.init();
cSeal.print(34);
cSeal.toInt("aaaa");
}
'C++' 카테고리의 다른 글
[C++] 6월 23일 코딩 테스트 수업 (0) | 2025.06.23 |
---|---|
[C++] 6월 20일 코딩 테스트 수업 (0) | 2025.06.20 |
[C++] 6월 16일 코딩 테스트 수업 (0) | 2025.06.16 |
[C++] 6월 13일 코딩 테스트 수업 (0) | 2025.06.13 |
[C++] 6월 11일 코딩 테스트 수업 (0) | 2025.06.11 |