zl程序教程

您现在的位置是:首页 >  其他

当前栏目

【华为OD机试】1025 - 字符串加解密

华为 字符串 机试 OD 加解密
2023-09-14 09:05:30 时间


🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈
 
🍂个人博客首页: KJ.JK
 
💖系列专栏:华为OD机试(Java&Python&C语言)

一、题目


🔸题目描述

对输入的字符串进行加解密,并输出。
加密方法为:
当内容是英文字母时则用该英文字母的后一个字母替换,同时字母变换大小写,如字母a时则替换为B;字母Z时则替换为a;
当内容是数字时则把该数字加1,如0替换1,1替换2,9替换0;
其他字符不做变化。
解密方法为加密的逆过程。


🔸输入输出

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


🔸样例1

输入
abcdefg
BCDEFGH



输出
BCDEFGH
abcdefg


二、代码参考

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

--------------------------------------------------------



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


--------------------------------------------------------------


#include <stdio.h>
#include <string.h>

//以下是Senky的代码:
//a的ASCII:97
//A的ASCII:65
//0的ASCII:48
void code(char* str, int len) {//加密
    for (int i = 0; i < len; i++) {
        if (str[i] >= 'a' && str[i] < 'z') { //小写字母a-y
            str[i] = str[i] + 'A' - 'a' + 1; //先转大写,再后移
        } else if (str[i] == 'z') { //z单独处理
            str[i] = 'A';
        } else if (str[i] >= 'A' && str[i] < 'Z') { //大写字母A-Y
            str[i] = str[i] + 'a' - 'A' + 1; //先转小写,再后移
        } else if (str[i] == 'Z') { //大写字母Z单独处理
            str[i] = 'a';
        } else if (str[i] >= '0' && str[i] < '9') { //数字0-8
            str[i] = str[i] + 1; //数字后移
        } else if (str[i] == '9') str[i] = '0'; //数字9单独处理
    }
}
void decode(char* str, int len) { //解密
    for (int i = 0; i < len; i++) {
        if (str[i] > 'a' && str[i] <= 'z') { //小写字母b-z
            str[i] = str[i] + 'A' - 'a' - 1; //先转大写,再前移
        } else if (str[i] == 'a') { //z单独处理
            str[i] = 'Z';
        } else if (str[i] > 'A' && str[i] <= 'Z') { //大写字母B-Z
            str[i] = str[i] + 'a' - 'A' - 1; //先转小写,再前移
        } else if (str[i] == 'A') { //大写字母A单独处理
            str[i] = 'z';
        } else if (str[i] > '0' && str[i] <= '9') { //数字1-9
            str[i] = str[i] - 1; //数字前移
        } else if (str[i] == '0') str[i] = '9'; //数字0单独处理
    }
}

void start() {
    char str[1001];//要加密的字符
    char st[1001];//要解密的字符

    scanf("%[^\n]\n%s\n", str, st);//读取字符串
 
    int len = strlen(str);//密码长度
    code(str, len); //加密
    printf("%s\n", str);


    len = strlen(st);//密码长度
    decode(st, len); //解密
    printf("%s\n", st);
}

int main() {
    start();
    return 0;//编辑于2022/10/14
}


请添加图片描述


作者:KJ.JK

文章对你有所帮助的话,欢迎给个赞或者 star,你的支持是对作者最大的鼓励,不足之处可以在评论区多多指正,交流学习