題目描述:
對於任意整數n,轉化成x進制的表達形式
算法分析:
算法實現:
//十進制正整數n到base進制的轉換
static const char baseNum[] =
{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
void convert ( std::stack<char>& S, __int64 n, int base )
{
while (n > 0)
{
int remainder = n % base;
S.push(baseNum[remainder]);
n = n / base;
}
}
void convertR ( std::stack<char>& S, __int64 n, int base )
{
if (n > 0)
{
int remainder = n % base;
S.push(baseNum[remainder]);
n = n / base;
convertR(S, n, base);
}
}
void main()
{
int testnum = 5864;
std::stack<char> rnt;
convert(rnt,testnum,8);//13350
while (!rnt.empty())
{
std::cout << rnt.top();
rnt.pop();
}
std::cout << std::endl;
convert(rnt,testnum,2);//1011011101000
while (!rnt.empty())
{
std::cout << rnt.top();
rnt.pop();
}
}
算法分析:
算法名稱 | 時間複雜度(平均) | 時間複雜度(最壞) | 時間複雜度(最好) | 空間複雜度 |
---|---|---|---|---|