题解 | 字符串加解密

def encrypt(s):
    result = ""
    for c in s:
        if c.isalpha():
            # For letters: shift forward 1 and swap case
            if c == "z":
                result += "A"
            elif c == "Z":
                result += "a"
            else:
                next_char = chr(ord(c) + 1)
                result += next_char.swapcase()
        elif c.isdigit():
            # For numbers: add 1, 9 becomes 0
            result += str((int(c) + 1) % 10)
    return result

def decrypt(t):
    result = ""
    for c in t:
        if c.isalpha():
            # For letters: shift backward 1 and swap case
            if c == "a":
                result += "Z"
            elif c == "A":
                result += "z"
            else:
                prev_char = chr(ord(c) - 1)
                result += prev_char.swapcase()
        elif c.isdigit():
            # For numbers: subtract 1, 0 becomes 9
            result += str((int(c) + 10 - 1) % 10)
    return result

# Read input
s = input()
t = input()

# Output encrypted and decrypted strings
print(encrypt(s))
print(decrypt(t))

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务