PAT 乙级1048 数字加密 python

题目
在这里插入图片描述

思路
值得注意的测试点是:
对于固定加密用正整数A长度长于要加密的对象B时,需要对B进行相应的补零操作
当固定加密用正整数A长度小于要加密的对象B时,直接保留B长出的字符片段
代码

alpha={10:'J',11:'Q',12:'K'}
key_,password=input().split(' ')
strat=len(password)-len(key_)
if strat<0: #当要加密的数字串没有固定密钥的长度时,需要进行补零操作再运算
    password='0'*abs(strat)+password
    strat=0

ji_or_ou=1
result_=password[0:strat] #当要加密的数字串长于固定密钥的长度时,直接保留原密文

for i in range(len(password)-1,strat-1,-1):
    if ji_or_ou==1: #奇数位时
        temp=(int(password[i]) + int(key_[i - strat]))%13
        if temp>9:
            temp=alpha[temp]
        result_=result_[0:strat]+str(temp)+result_[strat:]
        ji_or_ou=-ji_or_ou

    else: #偶数位时
        temp = int(password[i] )-int(key_[i - strat])
        if temp < 0:
            temp =temp+10
        result_=result_[0:strat]+str(temp)+result_[strat:]
        ji_or_ou = -ji_or_ou

print(result_)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值