【华为机考】【字符串加解密】Jave Python C++ C

描述:

对输入的字符串进行加解密,并输出。
加密方法为:
当内容是英文字母时则用该英文字母的后一个字母替换,同时字母变换大小写,如字母a时则替换为B;字母Z时则替换为a;当内容是数字时则把该数字加1,如0替换1,1替换2,9替换0;其他字符不做变化。
解密方法为加密的逆过程。
数据范围:输入的两个字符串长度满足
1≤n≤1000 ,保证输入的字符串都是只由大小写字母或者数字组成

输入描述:

第一行输入一串要加密的密码
第二行输入一串加过密的密码
输出描述:
第一行输出加密后的字符
第二行输出解密后的字符****

示例1

输入:

abcdefg
BCDEFGH

输出:

BCDEFGH
abcdefg

知识点:

字符串,模拟过程

Python 版本

解题思路:

使用num1和num2做大小写数字的转换,使用s接受num1和num2加解密后的字符,如果不属于num1和num2的字符保持不变即可

def cherk(a, b):
    num1 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
    num2 = 'BCDEFGHIJKLMNOPQRSTUVWXYZAbcdefghijklmnopqrstuvwxyza1234567890'
    s = []
    if b == 1:
        for i in a:
            if i in num1:
                s.append(num2[num1.index(i)]) //按照num1中i的索引在num2中取出对应的值,存入s中。
            else:
                s.append(i)  //如果不属于num1,即是特殊字符,不在处理直接存入s中即可;解密和加密逻辑一样
        return ''.join(s)
    if b == 0:
        for i in a:
            if i in num2:
                s.append(num1[num2.index(i)])
            else:
                s.append(i)
        return ''.join(s)
 
 
while True:
    try:
        m1 = input()
        m2 = input()
        print(cherk(m1, 1))
        print(cherk(m2, 0))
    except:
        break

Jave版本

解题思路:

用多分支判断将字符对应转换

import java.util.Scanner;
 
public class Main{
    
    
    public static void main(String[] args){
    
    
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
    
    
            System.out.println(encode(in.nextLine()));
            System.out.println(decode(in.nextLine()));
        }
    }
 
    //加密函数
    private static String encode(String code){
    
    
        char[] t = code.toCharArray();    //将String对象转换为字符数组
        for(int i=0; i < t.length; i++){
    
    
            if(t[i]>='a' && t[i]<'z')
                t[i] = (char)(t[i] - 'a' + 'A' + 1);
            else if(t[i] == 'z')
                t[i] = 'A';
            else if(t[i]>='A' && t[i]<'Z')
                t[i] = (char)(t[i] - 'A' + 'a' + 1);
            else if(t[i] == 'Z')
                t[i] = 'a';
            else if(t[i]>='0' && t[i]<'9')
                t[i] = (char)(t[i]+1);
            else if(t[i] == '9')
                t[i] = '0';
        }
        return String.valueOf(t);
    }
 
    //解密函数
    private static String decode(String password){
    
    
        char[] t = password.toCharArray();
        for(int i=0; i < t.length; i++){
    
    
            if(t[i]>'a' && t[i]<='z')
                t[i] = (char)(t[i] - 'a' + 'A' - 1);
            else if(t[i] == 'a')
                t[i] = 'Z';
            else if(t[i]>'A' && t[i]<='Z')
                t[i] = (char)(t[i] - 'A' + 'a' - 1);
            else if(t[i] == 'A')
                t[i] = 'z';
            else if(t[i]>'0' && t[i]<='9')
                t[i] = (char)(t[i]-1);
            else if(t[i] == '0')
                t[i] = '9';
        }
        return String.valueOf(t);
    }
}

C++ 版本

解题思路:

构建哈希表

#include <iostream>
using namespace std;
int main(){
    
    
    string str1, str2;
    cin >> str1 >> str2;
    string a("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
    string b("BCDEFGHIJKLMNOPQRSTUVWXYZAbcdefghijklmnopqrstuvwxyza1234567890");
    for(int i = 0; i < str1.size(); ++i)
        str1[i] = b[a.find_last_of(str1[i])];
    for(int i = 0; i < str2.size(); ++i)
        str2[i] = a[b.find_last_of(str2[i])];
    cout << str1 << endl << str2;
}

C 语言版本

解题思路:

用多分支判断将字符对应转换

#include <string.h>
 
int main()
{
    
    
    int i;
    char str[1002]={
    
    0},str2[1002]={
    
    0};
    scanf("%s",str);
    scanf("%s",str2);
     
    for(i=0;i<strlen(str);i++)
    {
    
    
        if(str[i]<90&&str[i]>=65)
            str[i]+=33;
        else if(str[i]=='Z')
            str[i]='a';
        else if(str[i]<122&&str[i]>=97)
            str[i]-=31;
        else if(str[i]=='z')
            str[i]='A';
        else if(str[i]<57&&str[i]>=48)
            str[i]+=1;
        else if(str[i]=='9')
            str[i]='0';
    }
    for(i=0;i<strlen(str2);i++)
    {
    
    
        if(str2[i]<=90&&str2[i]>65)
            str2[i]+=31;
        else if(str2[i]=='A')
            str2[i]='z';
        else if(str2[i]<=122&&str2[i]>97)
            str2[i]-=33;
        else if(str2[i]=='a')
            str2[i]='Z';
        else if(str2[i]<=57&&str2[i]>48)
            str2[i]-=1;
        else if(str2[i]=='0')
            str2[i]='9';
    }
    printf("%s\n",str);
    printf("%s\n",str2);
}

猜你喜欢

转载自blog.csdn.net/DFCED/article/details/129469341
今日推荐