python华为826笔试题第一题

二进制的变换:

大致就是数字一个数字的二进制的表示,首先进行两两位置的交换。

[1,2,3,4] ==> [2,1,4,3]   [1,0,1,0]==> [0,1,0,1]  

然后A B C    A的最后两位移动到B上,B的最后两位移动到C上,C的最后两位移动到A上,循环移位。

思路不难,按照步骤进行即可。

#输入 

1 2

输出:

1073741824 2147483648


from typing import List

# 两两交换数字
def fun2(list2):
    res = []
    for i in range(0, len(list2)-1, 2):
        res.append(list2[i+1])
        res.append(list2[i])
    return res


# 填充
def fun3(n):
    return bin(n)[2:].rjust(32, '0')


list2 = list(map(int, input().split(" ")))
res = []
for i in list2:
    res.append("".join(fun2(list(fun3(i)))))

temp = "".join(res)

temp2 = temp[-2:]+temp[:-2]


res2 = []
while len(temp2) > 0:
    res2.append(temp2[:32])
    temp2 = temp2[32:]

res3 = []
for i in range(len(res2)):
    res3.append(int(res2[i], 2))

print(" ".join(map(str, res3)))

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值