进制转换
9/2
4 1
2 0
1 0
0 1
529
264 1
132 0
66 0
33 0
16 1
8 0
4 0
2 0
1 0
0 1
32
16 0
8 0
4 0
2 0
1 0
0 1
数据结构:栈
练习题
A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.
For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. The number 32 has binary representation 100000 and has no binary gaps.
Write a function:
def solution(N)
that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.
For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5. Given N = 32 the function should return 0, because N has binary representation '100000' and thus no binary gaps.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..2,147,483,647].
def solution(N): def conversionOfNumberSystemsFromDecimalism(decimalismNum, base): stackContainer = [] N = decimalismNum b = base start = N while True: m = start % b start = int(start / b) stackContainer.append(m) if start == 0: break return stackContainer stackContainer = conversionOfNumberSystemsFromDecimalism(N, 2) if stackContainer.count(1) <= 1: return 0 else: l = stackContainer[stackContainer.index(1):] s = ''.join([str(i) for i in l]) return max([len(i) for i in s.split('1')])
INPUT ori input_base output_base COMPUTE def to10(input_base,i) return i10 ori10=to10(i) for i=1;i++ m=mod(ori10,to10(output_base,i)) res+=m*output_base^(i-1) ori10-=m if ori10== 0: break OUTPUT res
求余数运算,每运算一次,原数值减少,获取结果值的一次位;
以10进制做为桥梁
input_base, input_str, output_base =8, '103022323', 4
output_str_base_index = []
input_10, input_str_len = 0, len(input_str)
for i in range(0, input_str_len, 1):
input_10 += input_base ** (input_str_len - 1 - i) * int(input_str[i]) # int(MAP[input_str[i]])
print('input_10:', input_10)
i, c = 1, input_10
while True:
m = c % (output_base ** i) / (output_base ** (i - 1))
output_str_base_index = [m] + output_str_base_index
c -= m * (output_base ** (i - 1))
i += 1
if c == 0:
break
print(output_str_base_index)
1 AB2021011600001 2 AB2021011600002 3 AB2021011600003 4 AB2021011600004 5 AB2021011600005 6 AB2021011600006 7 AB2021011600007 8 AB2021011600008 9 AB2021011600009 10 AB202101160000A 11 AB202101160000B 12 AB202101160000C 13 AB202101160000D 14 AB202101160000E 15 AB202101160000F 16 AB202101160000G 17 AB202101160000H 18 AB202101160000I 19 AB202101160000J 20 AB202101160000K 21 AB202101160000L 22 AB202101160000M 23 AB202101160000N 24 AB202101160000O 25 AB202101160000P 26 AB202101160000Q 27 AB202101160000R 28 AB202101160000S 29 AB202101160000T 30 AB202101160000U 31 AB202101160000V 32 AB202101160000W 33 AB202101160000X 34 AB202101160000Y 35 AB202101160000Z 100 AB2021011600010 101 AB2021011600011 102 AB2021011600012 320901 AB2021011600W91 320900 AB2021011600W90 320835 AB2021011600W8Z 320834 AB2021011600W8Y 320833 AB2021011600W8X 320832 AB2021011600W8W 320831 AB2021011600W8V 320830 AB2021011600W8U 320829 AB2021011600W8T 320828 AB2021011600W8S 320827 AB2021011600W8R 320826 AB2021011600W8Q 320825 AB2021011600W8P 320824 AB2021011600W8O 320823 AB2021011600W8N 320822 AB2021011600W8M 320821 AB2021011600W8L 320820 AB2021011600W8K 320819 AB2021011600W8J 320818 AB2021011600W8I 320817 AB2021011600W8H 320816 AB2021011600W8G 320815 AB2021011600W8F 320814 AB2021011600W8E 320813 AB2021011600W8D 320812 AB2021011600W8C 320811 AB2021011600W8B 320810 AB2021011600W8A 320809 AB2021011600W89 320808 AB2021011600W88 320807 AB2021011600W87 320806 AB2021011600W86 320805 AB2021011600W85 320804 AB2021011600W84 320803 AB2021011600W83 320802 AB2021011600W82 320801 AB2021011600W81 320800 AB2021011600W80 320735 AB2021011600W7Z
def g(v): carry = False arr = [] v1 = v for i in range(0, 5, 1): mod = int(v1 % 100) v1 = v1 / 100 if i == 0 or carry: mod += 1 if mod <= 35: carry = False arr.append(int(mod)) elif mod == 36: carry = True arr.append(0) r = '' s = '' f = lambda i: str(i) if len(str(i)) == 2 else '0' + str(i) for i in range(0, 5, 1): ii = arr[i] r = f(ii) + r for i in range(0, 5, 1): ii = arr[i] s = m[ii] + s return (r, s)
10进制转二进制
package main import "fmt" func main() { var g = func(x, y int) (z int) { z = 1 for i := 0; i < y; i++ { z *= x } return } var f = func(i int) (arr [64]int) { arr = [64]int{} for j := 63; j > -1; j-- { balance := i - g(2, j) if balance >= 0 { arr[j] = 1 i = balance } } return } i := 1024 fmt.Println(f(i)) }