题解 | #字符串加解密#

字符串加解密

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

  1. 直接判断求解
while True:
    try:
        s1=input()
        s2=input()
        s1_new = ''
        s2_new = ''
        for i in s1:
            if i.isupper():
                if i == 'Z':
                    s1_new+='a'
                else:
                    s1_new += chr(ord(i)+1).lower()
            elif i.islower():
                if i == 'z':
                    s1_new += 'A'
                else:
                    s1_new += chr(ord(i)+1).upper()
            elif i.isdigit():
                s1_new += str(int(i)+1)[-1]
            else:
                s1_new += i
        print(s1_new)
        
        for i in s2:
            if i.isupper():
                if i == 'A':
                    s2_new +='z'
                else:
                    s2_new += chr(ord(i) - 1).lower()
            elif i.islower():
                if i == 'a':
                    s2_new += 'Z'
                else:
                    s2_new += chr(ord(i) - 1).upper()
            elif i.isdigit():
                if i == '0':
                    s2_new += '9'
                else:
                    s2_new += str(int(i)-1)
            else:
                s2_new += i
        print(s2_new)

    except:
        break
                
            
                
  1. 查表法
def check(a,b):
    L1 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    L2 = "BCDEFGHIJKLMNOPQRSTUVWXYZAbcdefghijklmnopqrstuvwxyza1234567890"
    result = ""
    if b == 1:
        for i in a:
            result += L2[L1.index(i)]
    elif b == -1:
        for i in a:
            result += L1[L2.index(i)]
    return result
while True:
    try:
        print(check(input(),1))
        print(check(input(), -1))

    except:
        break

全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 18:54
点赞 评论 收藏
分享
1 5 评论
分享
牛客网
牛客企业服务