题解 | #字符串加解密#

字符串加解密

https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a

import sys
#由于编码和解码过程互逆,所以可以设计一个移n位的单字函数,进行复用。
def code_s(st,n): #n为1时为题目中的加密过程,n=-1则为解密,取其他值亦可。
    if 97 <= int(ord(st)) <= 122:  #a-z为97到122
        res = chr(65 + (int(ord(st)) - 97 + int(n)) % 26) #26个字母剩余类向前n位轮换
    elif 65 <= int(ord(st)) <= 90: #A-Z为65到90
        res = chr(97 + (int(ord(st)) - 65 + int(n)) % 26) #26个字母剩余类向前n位轮换
    elif 0 <= int(st) <= 9:  #数字
        res = str((int(st) + int(n))%10) #移位后mod10取出个位数
    return res
def code(st,n): #遍历函数,对各位进行加密解密,并组合结果。
    res = ''
    for s in st:
        res += code_s(s,n)
    return res
st1 = input()
st2 = input()
print(code(st1,1))
print(code(st2,-1))

全部评论

相关推荐

2025-12-04 15:36
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务