A = [1, 1, 0, 0, 1, 1, 0, 1] B = [1, 1, 1, 0, 1, 0, 1, 1] def sumbinary(A, B): A.reverse() B.reverse() C = [] temp = 0 length = len(A) for i in range(0, length): C.append(A[i] + B[i] + temp) if C[i] > 1: C[i] %= 2 temp = 1 else: temp = 0 print(C[i]) C.append(temp) C.reverse() return C print(sumbinary(A, B))
算法导论:两个n位的二进制整数相加问题之python实现
最新推荐文章于 2021-10-07 08:06:02 发布