题解 | #字符串加密#

字符串加密

https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3

题目原文都写了保证输入全是小写,输出也要小写,好多人用了一堆工具类还对大小写费半天劲,不知道他们在干嘛。。

我解法很简单,就是比对、拼接字符串

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            String key = in.nextLine().toLowerCase();//密钥
            int count = 0;
            while (count < key.length()) { //密钥去重
                key = key.substring(0, count + 1) + key.substring(count + 1).replace(
                          Character.toString(key.charAt(count)), "");
                count++;
            }
            String str = in.nextLine();//明文
            String dic = "abcdefghijklmnopqrstuvwxyz";          //加密字典
            String password = "abcdefghijklmnopqrstuvwxyz";     //解密字典
            //加密字典补完
            for (int i = 0 ; i < key.length(); i++) {
                dic = dic.replace(Character.toString(key.charAt(i)), "");
            }
            dic = key + dic;
            //对照解密字典输出加密内容
            for (int i = 0; i < str.length(); i++) {
                for (int j = 0; j < password.length(); j++) {
                    if (password.charAt(j) == str.charAt(i))System.out.print(dic.charAt(j));
                }
            }
        }
    }
}

全部评论

相关推荐

但听说转正率很低,我现在有在实习了,好纠结要不要去
熬夜脱发码农:转正率低归低,但是实习的经历你可以拿着,又不是说秋招不准备了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务