更新时间2019-07-13 06:41:26
进制转换(一)
输入十进制的正整数N(N<1000000),K(K<=16),请把N化为K进制后输出。有多组数据。
样例输入
10 2
10 16
样例输出
1010
A
是C++的?以下是我OJ中常用的,多次有于OJ,可转换任何进制,主程序自己写下
void m_itoa( char *buf, int i, unsigned char base)
{
unsigned int rem;
unsigned char *s, length = 0;
s = ( unsigned char *)buf;
if(i == 0)
*s++ = '0';
else
{
if(i < 0)
{
if(base <= 10)
{
*buf++ = '-';
s =( unsigned char *) buf;
}
i = -i;
}
while(i)
{
++length;
rem = i % base;
if(rem <= 10)
*s++ = rem + '0';
else if(base <= 36)
*s++ = rem - 10 + 'A';
i /= base;
}
for(base = 0; base < length / 2; base++)
{
*(buf + length) = *(buf + base);
*(buf + base) = *(buf + (length - base - 1));
*(buf + (length - base - 1)) = *(buf + length);
}
}
*s = 0;
}
样例是二进制转换成十进制和十六进制转换成十进制。