For each case, the input file contains a positive integer n (n<=20000).
For each case, you should output the exponential form of n an a single line.Note that,there should not be any additional white spaces in the line.
1315
2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)
简单易懂,最简洁实现。一个转换函数,递归实现
def convenBin(num): numBin = bin(num).replace('0b','') #转换成2进制 numPower = len(numBin)-1 #对应长度的2进制次幂 result = [] while numBin: if numBin[0] == '1': #如果该二进制位为1我们才加入结果 if numPower >= 2: #次幂大于等于2我们要递归它 result.append('2(%s)'% convenBin(numPower)) elif numPower == 1: #1和0不能通用解决,就单独列出来 result.append('2') else: result.append('2(0)') numPower -= 1 numBin = numBin[1:] return '+'.join(result) #返回用+相接的结果 while True: try: print(convenBin(int(input))) except Exception: break