题解 | #字符串加解密#
字符串加解密
https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<String> LIST = new ArrayList<>();
Collections.addAll(LIST, "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
"L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" );
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list, "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" );
ArrayList<String> NUM = new ArrayList<>();
Collections.addAll(NUM, "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" );
String encode = sc.next();
String decode = sc.next();
for (String en : encode.split("" )) {
if (LIST.contains(en)) {
if (en.matches("[A-Y]" )) en = list.get(list.indexOf(en.toLowerCase()) + 1);
if (en.matches("Z" )) en = list.get(0);
} else if (list.contains(en)) {
if (en.matches("[a-y]" )) en = LIST.get(LIST.indexOf(en.toUpperCase()) + 1);
if (en.matches("z" )) en = LIST.get(0);
} else {
if (en.matches("[0-8]" )) en = NUM.get(NUM.indexOf(en) + 1);
else en = NUM.get(0);
}
System.out.print(en);
}
System.out.println();
for (String de : decode.split("" )) {
if (LIST.contains(de)) {
if (de.matches("[B-Z]" )) de = list.get(list.indexOf(de.toLowerCase()) - 1);
if (de.matches("A" )) de = list.get(25);
} else if (list.contains(de)) {
if (de.matches("[b-z]" )) de = LIST.get(LIST.indexOf(de.toUpperCase()) - 1);
if (de.matches("a" )) de = LIST.get(25);
} else {
if (de.matches("[1-9]" )) de = NUM.get(NUM.indexOf(de) - 1);
else de = NUM.get(9);
}
System.out.print(de);
}
}
}
查看10道真题和解析