华为OD机试真题 - 字符串加解密 - 2024E卷(Java、Python、JavaScript、C、C++合集)

本文详细描述了一种字符串加密方法,通过替换字母(大小写)和数字(加1),然后提供Java、JavaScript、Python和C/C++的实现代码,以及对应的解密过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、题目描述

对输入的字符串进行加解密,并输出。

加密方法为:

当内容是英文字母时则用该英文字母的后一个字母替换,同时字母变换大小写,如字母a时则替换为B;字母Z时则替换为a;

当内容是数字时则把该数字加1,如0替换1,1替换2,9替换0;

其他字符不做变化。

解密方法为加密的逆过程。

数据范围:输入的两个字符串长度满足 1≤n≤1000 ,保证输入的字符串都是只由大小写字母或者数字组成

二、输入描述

第一行输入一串要加密的密码;
第二行输入一串加过密的密码;

三、输出描述

第一行输出加密后的字符;
第二行输出解密后的字符;

四、解题思路

  1. 读取输入的要加密的密码字符串 code;
  2. 将 code 转换为字符数组 t;
  3. 遍历 t 数组,对每个字符进行如下操作:
    • 如果字符是小写字母且不是字母z,则将字符替换为后一个大写字母;
    • 如果字符是字母z,则将字符替换为大写字母A;
    • 如果字符是大写字母且不是字母Z,则将字符替换为后一个小写字母;
    • 如果字符是字母Z,则将字符替换为小写字母a;
    • 如果字符是数字且不是数字9,则将字符替换为后一个数字;
    • 如果字符是数字9,则将字符替换为数字0;
  4. 将加密后的字符数组 t 转换为字符串并返回;
  5. 读取输入的加过密的密码字符串 password;
  6. 将 password 转换为字符数组 t;
  7. 遍历 t 数组,对每个字符进行如下操作:
    • 如果字符是小写字母且不是字母a,则将字符替换为前一个大写字母;
    • 如果字符是字母a,则将字符替换为大写字母Z;
    • 如果字符是大写字母且不是字母A,则将字符替换为前一个小写字母;
    • 如果字符是字母A,则将字符替换为小写字母z;
    • 如果字符是数字且不是数字0,则将字符替换为前一个数字;
    • 如果字符是数字0,则将字符替换为数字9;
  8. 将解密后的字符数组 t 转换为字符串并返回。

五、Java算法源码

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();
    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);
}

六、JavaScript算法源码

// 创建一个读取标准输入的函数,这里我们使用prompt代替  
function readLine() {  
    return prompt("请输入一行文本:");  
}  
  
// 主函数,持续读取输入直到用户停止  
function main() {  
    while (true) {  
        const input = readLine();  
        if (input === null) { // 用户可能点击了取消,或者没有输入直接提交  
            break;  
        }  
        const encoded = encode(input);  
        console.log(encoded);  
        const decoded = decode(input);  
        console.log(decoded);  
    }  
}  
  
// 加密函数  
function encode(code) {  
    const t = code.split('').map(c => c.charCodeAt(0)); // 将字符串转换为字符数组(使用charCodeAt得到字符的Unicode编码)  
    for (let i = 0; i < t.length; i++) {  
        if (t[i] >= 97 && t[i] < 123) // 小写字母  
            t[i] = (t[i] - 97 + 65 + 1).toString().charCodeAt(0);  
        else if (t[i] === 122) // 字母z  
            t[i] = 65; // 转换为大写A  
        else if (t[i] >= 65 && t[i] < 91) // 大写字母  
            t[i] = (t[i] - 65 + 97 + 1).toString().charCodeAt(0);  
        else if (t[i] === 90) // 字母Z  
            t[i] = 97; // 转换为小写a  
        else if (t[i] >= 48 && t[i] < 58) // 数字  
            t[i] = (t[i] + 1).toString().charCodeAt(0);  
        else if (t[i] === 57) // 数字9  
            t[i] = 48; // 转换为数字0  
    }  
    return String.fromCharCode(...t); // 使用fromCharCode将Unicode编码转换回字符,并拼接成字符串  
}  
  
// 解密函数  
function decode(password) {  
    const t = password.split('').map(c => c.charCodeAt(0)); // 将字符串转换为字符数组(使用charCodeAt得到字符的Unicode编码)  
    for (let i = 0; i < t.length; i++) {  
        if (t[i] > 97 && t[i] <= 122) // 小写字母  
            t[i] = (t[i] - 97 + 65 - 1).toString().charCodeAt(0);  
        else if (t[i] === 97) // 字母a  
            t[i] = 90; // 转换为大写Z  
        else if (t[i] > 65 && t[i] <= 90) // 大写字母  
            t[i] = (t[i] - 65 + 97 - 1).toString().charCodeAt(0);  
        else if (t[i] === 65) // 字母A  
            t[i] = 122; // 转换为小写z  
        else if (t[i] > 48 && t[i] <= 57) // 数字  
            t[i] = (t[i] - 1).toString().charCodeAt(0);  
        else if (t[i] === 48) // 数字0  
            t[i] = 57; // 转换为数字9  
    }  
    return String.fromCharCode(...t); // 使用fromCharCode将Unicode编码转换回字符,并拼接成字符串  
}  
  
// 调用主函数  
main();

七、Python算法源码

# 导入Python的内置输入函数  
import sys  
  
# 主函数  
def main():  
    # 循环读取标准输入,直到输入结束  
    for line in sys.stdin:  
        # 调用encode函数进行编码,并打印结果  
        print(encode(line.strip()))  
        # 调用decode函数进行解码,并打印结果  
        print(decode(line.strip()))  
  
# 加密函数  
def encode(code):  
    # 将字符串转换为字符数组  
    t = list(code)  
    for i in range(len(t)):  
        # 如果是小写字母  
        if 'a' <= t[i] <= 'z':  
            # 转换为对应的大写字母,并向后移动一位  
            t[i] = chr((ord(t[i]) - ord('a') + ord('A') + 1)  
        # 如果是字母'z'  
        elif t[i] == 'z':  
            # 转换为大写字母'A'  
            t[i] = 'A'  
        # 如果是大写字母  
        elif 'A' <= t[i] <= 'Z':  
            # 转换为对应的小写字母,并向后移动一位  
            t[i] = chr((ord(t[i]) - ord('A') + ord('a') + 1)  
        # 如果是字母'Z'  
        elif t[i] == 'Z':  
            # 转换为小写字母'a'  
            t[i] = 'a'  
        # 如果是数字  
        elif '0' <= t[i] <= '9':  
            # 数字向后移动一位  
            t[i] = chr((ord(t[i]) + 1)  
        # 如果是数字'9'  
        elif t[i] == '9':  
            # 转换为数字'0'  
            t[i] = '0'  
    # 将字符数组转换回字符串  
    return ''.join(t)  
  
# 解密函数  
def decode(password):  
    # 将字符串转换为字符数组  
    t = list(password)  
    for i in range(len(t)):  
        # 如果是小写字母(除'a'外)  
        if 'b' <= t[i] <= 'z':  
            # 转换为对应的大写字母,并向前移动一位  
            t[i] = chr((ord(t[i]) - ord('a') + ord('A') - 1)  
        # 如果是字母'a'  
        elif t[i] == 'a':  
            # 转换为大写字母'Z'  
            t[i] = 'Z'  
        # 如果是大写字母(除'A'外)  
        elif 'B' <= t[i] <= 'Z':  
            # 转换为对应的小写字母,并向前移动一位  
            t[i] = chr((ord(t[i]) - ord('A') + ord('a') - 1)  
        # 如果是字母'A'  
        elif t[i] == 'A':  
            # 转换为小写字母'z'  
            t[i] = 'z'  
        # 如果是数字(除'0'外)  
        elif '1' <= t[i] <= '9':  
            # 数字向前移动一位  
            t[i] = chr((ord(t[i]) - 1)  
        # 如果是数字'0'  
        elif t[i] == '0':  
            # 转换为数字'9'  
            t[i] = '9'  
    # 将字符数组转换回字符串  
    return ''.join(t)  
  
# 调用主函数  
if __name__ == "__main__":  
    main()

八、C算法源码

#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include <ctype.h> // 引入ctype.h头文件以使用字符处理函数  
  
// 主函数  
int main(int argc, char *argv[]) {  
    char line[1024]; // 定义足够大的缓冲区来存储输入行  
    while (fgets(line, sizeof(line), stdin)) { // 从标准输入读取一行  
        line[strcspn(line, "\n")] = 0; // 移除行尾的换行符  
        printf("%s\n", encode(line)); // 调用加密函数并打印结果  
        printf("%s\n", decode(line)); // 调用解密函数并打印结果  
    }  
    return 0;  
}  
  
// 加密函数  
char *encode(char *code) {  
    int length = strlen(code); // 获取输入字符串的长度  
    char *result = (char *)malloc((length + 1) * sizeof(char)); // 分配结果字符串所需的内存  
    if (result == NULL) {  
        perror("Memory allocation failed");  
        exit(EXIT_FAILURE);  
    }  
    for (int i = 0; i < length; i++) {  
        if (islower(code[i])) { // 如果是小写字母  
            result[i] = code[i] - 'a' + 'A' + 1;  
        } else if (code[i] == 'z') { // 如果是字母'z'  
            result[i] = 'A';  
        } else if (isupper(code[i])) { // 如果是大写字母  
            result[i] = code[i] - 'A' + 'a' + 1;  
        } else if (code[i] == 'Z') { // 如果是字母'Z'  
            result[i] = 'a';  
        } else if (isdigit(code[i])) { // 如果是数字  
            result[i] = code[i] - '0' + 1;  
            if (result[i] > '9') { // 处理数字回滚到'0'的情况  
                result[i] = '0';  
            }  
        }  
    }  
    result[length] = '\0'; // 确保结果字符串以空字符结尾  
    return result;  
}  
  
// 解密函数  
char *decode(char *password) {  
    int length = strlen(password); // 获取输入字符串的长度  
    char *result = (char *)malloc((length + 1) * sizeof(char)); // 分配结果字符串所需的内存  
    if (result == NULL) {  
        perror("Memory allocation failed");  
        exit(EXIT_FAILURE);  
    }  
    for (int i = 0; i < length; i++) {  
        if (isupper(password[i]) && password[i] != 'A') { // 如果是大写字母且不是'A'  
            result[i] = password[i] - 'A' + 'a' - 1;  
        } else if (password[i] == 'A') { // 如果是字母'A'  
            result[i] = 'z';  
        } else if (islower(password[i]) && password[i] != 'a') { // 如果是小写字母且不是'a'  
            result[i] = password[i] - 'a' + 'A' - 1;  
        } else if (password[i] == 'a') { // 如果是字母'a'  
            result[i] = 'Z';  
        } else if (isdigit(password[i])) { // 如果是数字  
            result[i] = password[i] - '0' - 1;  
            if (result[i] < '0') { // 处理数字回滚到'9'的情况  
                result[i] = '9';  
            }  
        }  
    }  
    result[length] = '\0'; // 确保结果字符串以空字符结尾  
    return result;  
}  

九、C++算法源码

#include <iostream>  
#include <string>  
using namespace std;  
  
// 主函数  
int main(int argc, char* argv[]) {  
    string line;  
    while (getline(cin, line)) { // 从标准输入读取一行  
        cout << encode(line) << endl; // 调用加密函数并打印结果  
        cout << decode(line) << endl; // 调用解密函数并打印结果  
    }  
    return 0;  
}  
  
// 加密函数  
string encode(string code) {  
    // 将字符串转换为字符数组  
    for (char& c : code) {  
        if (c >= 'a' && c < 'z')  
            c = static_cast<char>(c - 'a' + 'A' + 1); // 小写字母转换为大写字母并向后移动一位  
        else if (c == 'z')  
            c = 'A'; // 'z'转换为'A'  
        else if (c >= 'A' && c < 'Z')  
            c = static_cast<char>(c - 'A' + 'a' + 1); // 大写字母转换为小写字母并向后移动一位  
        else if (c == 'Z')  
            c = 'a'; // 'Z'转换为'a'  
        else if (c >= '0' && c < '9')  
            c = static_cast<char>(c + 1); // 数字向后移动一位  
        else if (c == '9')  
            c = '0'; // '9'回滚到'0'  
    }  
    return code; // 返回修改后的字符串  
}  
  
// 解密函数  
string decode(string password) {  
    // 将字符串转换为字符数组  
    for (char& c : password) {  
        if (c > 'a' && c <= 'z')  
            c = static_cast<char>(c - 'a' + 'A' - 1); // 小写字母转换为大写字母并向前移动一位  
        else if (c == 'a')  
            c = 'Z'; // 'a'转换为'Z'  
        else if (c > 'A' && c <= 'Z')  
            c = static_cast<char>(c - 'A' + 'a' - 1); // 大写字母转换为小写字母并向前移动一位  
        else if (c == 'A')  
            c = 'z'; // 'A'转换为'z'  
        else if (c > '0' && c <= '9')  
            c = static_cast<char>(c - 1); // 数字向前移动一位  
        else if (c == '0')  
            c = '9'; // '0'回滚到'9'  
    }  
    return password; // 返回修改后的字符串  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值