首页 > 试题广场 >

进制转换

[编程题]进制转换
  • 热度指数:34756 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
给定一个十进制数M,以及需要转换的进制数N。将十进制数M转化为N进制数

输入描述:
输入为一行,M(32位整数)、N(2 ≤ N ≤ 16),以空格隔开。


输出描述:
为每个测试实例输出转换后的数,每个输出占一行。如果N大于9,则对应的数字规则参考16进制(比如,10用A表示,等等)
示例1

输入

7 2

输出

111
def change(m,n):
    a = ''
    while m > 0:
        x = m % n
        a = chr(65 + x % 10) + a if x >= 10 else str(x) + a
        m //= n
    return a

编辑于 2019-11-14 01:21:53 回复(0)
MN = list(map(int, input().strip().split()))
nli = []
m = MN[0]
if m<0:
    sign = -1
    m = -m
else:
    sign = 1
n = MN[1]
while m>=n:
    nli.append(m%n)
    m = m//n
if m!=0:
    nli.append(m)
nli = nli[::-1]
if sign==-1:
    s = "-"
else:
    s = ""
for n in nli:
    if n<=9:
        s += str(n)
    else:
        s += chr(ord('A')+(n-10))
print(s)
编辑于 2019-08-27 10:09:44 回复(0)
# python3
def digit2N(digit, N):
    pos_flag = True if digit > 0 else False
    digit = abs(digit)
    adict = '0123456789ABCDEF'
    
    ret = ''
    while digit:
        ret = adict[digit % N] + ret
        digit = digit // N
    return ret if pos_flag else ('-'+ret)

input_num, N = map(int, input().split(' '))
ret = digit2N(input_num, N)
print(ret)

发表于 2019-06-04 01:15:28 回复(0)
def BaseN(M,N):
    res=""
    if M>0:
        while M:
            res="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[M%N]+res
            M//=N
            return res
    elif M==0:
        res="0"
    else:
        M=-M
        while M:
            res="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[M%N]+res
            M//=N
            return "-"+res   

发表于 2018-12-03 07:50:08 回复(0)
def TransferNum(m, n):
    s = ""
    table = "0123456789ABCDEF"
    a = m
    if m < 0:
         m = -m
   while m:
         s = table[m % n] + s
         m = m // n
   if a > 0:
         return s
   elif a == 0:
         return 0
   else:
         return "-" + s

m, n = map(int, input().split())
print(TransferNum(m, n))

发表于 2018-08-07 18:12:02 回复(0)
Python solution
def baseN(m, n):
    if m < 0:
        m = -m
        flag = True
    else:
        flag = False
    ans = ''
    while m > 0:
        each = divmod(m, n)[1]
        if each >= 10:
            ans += chr(65 + each - 10)
        else:
            ans += str(each)
        m //= n
    return '-'+ans[::-1] if flag else ans[::-1]

m, n = map(int, input().split())
print(baseN(m, n))

发表于 2018-07-31 13:56:46 回复(0)
import sys
a = list(map(int,sys.stdin.readline().split()))
b = []
if a[0]>0:     while a[0]/a[1] :         b.append(a[0]%a[1])         a[0] = (int(a[0]/a[1]))     c = b[::-1]     res = ''     for i in range(len(c)):         if c[i]==10:             c[i] = 'A'         elif c[i] ==11:             c[i] = 'B'         elif c[i] ==12:             c[i] = 'C'         elif c[i] ==13:             c[i] = 'D'         elif c[i] ==14:             c[i] = 'E'         elif c[i] ==15:             c[i] = 'F'         res = res +str(c[i])     print(res)
else:     a[0] = -a[0]     while a[0]/a[1] :         b.append(a[0]%a[1])         a[0] = (int(a[0]/a[1]))     c = b[::-1]     res = ''     for i in range(len(c)):         if c[i]==10:             c[i] = 'A'         elif c[i] ==11:             c[i] = 'B'         elif c[i] ==12:             c[i] = 'C'         elif c[i] ==13:             c[i] = 'D'         elif c[i] ==14:             c[i] = 'E'         elif c[i] ==15:             c[i] = 'F'         res = res +str(c[i])     print('-'+res)


发表于 2018-06-26 17:41:11 回复(0)
import sys
M,N=list(map(int,sys.stdin.readline().strip().split()))
mydict={10:'A',11:'B',12:'C',13:'D',14:'E',15:'F'}
ans=[]
flag=0#负数标志
if M<0:#M为负数时,flag置1,并将M变为正数
    flag=1
    M=-M
while M>0:
    res=M%N
    M=M//N
    if res>=10:#余数大于等于10时,映射为对应的字母
        ans.append(mydict[res])
    else:
        ans.append(str(res))
ans.reverse()#余数倒序排列
print('-'*flag+''.join(ans))#负数时flag为1,加上负号

发表于 2018-05-27 18:16:51 回复(0)

小心负数的坑就是了!!!

N,q = map(int,input().split())
dict_x = {10:'A',11:'B',12:'C',13:'D',14:'E',15:'F'}
ans = []
neg = False
if N<0:
    neg=True
    N = -N
while N>=q:
    ans.append(N%q)
    N = N//q
ans.append(N)
ans = [dict_x[x] if x > 9 else x for x in ans]
out = ''.join(list(map(str,ans))[::-1])
if neg==True:
    out = '-' + out
print(out)
发表于 2018-05-23 19:45:06 回复(0)

python解法

本來想用递归解的,结果超出最大深度,只能用循环做了。

def baseN(num, b):
    res = ""
    if num > 0:
        while num:
            res = "0123456789ABCDEFGHIGKLMNOPQRSTUVWXYZ"[num % b] + res
            num = num // b
        return res
    else:
        num = -num
        while num:
            res = "0123456789ABCDEFGHIGKLMNOPQRSTUVWXYZ"[num % b] + res
            num = num // b
        return "-" + res


a, b = map(int, input().split())
print(baseN(a, b))
发表于 2018-04-13 10:21:05 回复(2)
num =raw_input("")
M=int(num.split(" ")[0])
N=int(num.split(" ")[1])
arr=['A','B','C','D','E','F']
flag=True
if M<0:
    flag=False
    M=-M
snum=[]
while M!=0:
    temp=M%N
    M=M/N
    if temp>9:
        snum.append(arr[temp-10])
    else:
        snum.append(temp)
str1=''
snum=list(reversed(snum))
for i in xrange(len(snum)):
    str1=str1+str(snum[i])
if flag==False:
    print '-'+str1
else:
    print str1

发表于 2017-11-16 10:55:46 回复(0)
m, n = input().split(" ")
m = int(m)
n = int(n)
flag=True
str_result = ''
list_number = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']
while True:
    if m<0:
        m = -m
        flag = False
    tem = m % n
    m = (m-tem)/n
    str_result = list_number[int(tem)]+str_result
    if int(m) == 0:
        break
if flag:
print(str_result)
else:
    print('-'+str_result)
发表于 2017-09-07 00:25:37 回复(0)
import sys
import math
data=[int(i) for i in sys.stdin.readline().strip('\n').split()]
num=data[0]
if num>0:
    tag=0
else:
    tag=1
    num=abs(num)
system=data[1]
map1={10:'A',11:'B',12:'C',13:'D',14:'E',15:'F',16:'G'}
result=[]
while True:
    a=num%system
    if a in map1:
        result.append(map1[a])
    else:
        result.append(str(num%system))
    num=int(num/system)
    if num==0:
        break
if tag:
 print('-'+''.join(list(reversed(result))))
else:
    print(''.join(list(reversed(result))))
发表于 2017-08-31 16:09:48 回复(0)
"""存在bug,但是牛客测试平台还是过了,很low很初级的思路"""

mn = map(int,raw_input("").split(" "))
m = mn[0]
n = mn[1]
tm = 0
if m<0:
    tm = m*(-1)
else:
    tm = m
string = ""
while True:
    if tm/n>=1:
        ys = tm%n
        tm = tm/n
        if ys>9:
            if ys==10:
                temp = "A"
            elif ys==11:
                temp = "B"
            elif ys==12:
                temp = "C"
            elif ys==13:
                temp = "D"
            elif ys==14:
                temp = "E"
            elif ys==15:
                temp = "F"
        else:
            temp = str(ys)
        string = string+temp

    else:
        break

string = string + str(tm)
if m>0:
    print string[::-1]
else:
    print "-"+string[::-1]

发表于 2017-08-25 23:33:32 回复(0)
a = raw_input().split(' ')
m = int(a[0])
n = int(a[1])
index = [0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F']
if m>0:
    t = m
    n_str = ''
    while t>0:
        s = t/n
        y = t%n
        t = s
        n_str = str(index[y])+n_str
else:
    t = -m
    n_str = ''
    while t>0:
        s = t/n
        y = t%n
        t = s
        n_str = str(index[y])+n_str
    n_str = '-'+n_str
print n_str
发表于 2017-05-07 16:20:18 回复(0)