题解 | #字符串加密#
字符串加密
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)); } } } } }