C++

[C++] 6월 18일 코딩 테스트 수업

k-codestudy 2025. 6. 18. 17:43

프로그래머스 봉인된 주문서 

 

#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");

}