描述 | |
---|---|
知识点 | 字符串 |
运行时间限制 | 10M |
内存限制 | 128 |
输入 | 输入说明 |
输出 | 输出说明 |
样例输入 | abcdefg BCDEFGH |
样例输出 | BCDEFGH abcdefg |
/*本题采用的是把字母跟数字存在字符串中,然后根据规则从中取,最后组成输出的字符串
* */
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String aucPassword = sc.next(); // 要加密的字符串
String aucResult = sc.next(); //要解密的字符串
sc.close();
System.out.println(encrypt(aucPassword));
System.out.println(unEncrypt(aucResult));
}
private static String encrypt(String aucPassword) {
String allChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
String allNum = "0123456789";
StringBuilder stb = new StringBuilder();
char temp;
for (int i = 0; i < aucPassword.length(); i++) {
if (aucPassword.charAt(i) >= 'A' && aucPassword.charAt(i) <= 'Z') {
if (aucPassword.charAt(i) != 'Z') {
int flag = (int) (aucPassword.charAt(i) - 64);
temp = allChar.charAt(flag);
stb.append(temp);
} else {
stb.append('a');
}
}
if (aucPassword.charAt(i) >= 'a' && aucPassword.charAt(i) <= 'z') {
if (aucPassword.charAt(i) != 'z') {
int flag = (int) (aucPassword.charAt(i) - 70);
temp = allChar.charAt(flag);
stb.append(temp);
} else {
stb.append('A');
}
}
if (aucPassword.charAt(i) >= '0' && aucPassword.charAt(i) <= '9') {
if (aucPassword.charAt(i) != '9') {
int flag = (int) (aucPassword.charAt(i) - 47);
temp = allNum.charAt(flag);
stb.append(temp);
} else {
stb.append('0');
}
}
}
return stb.toString();
}
private static String unEncrypt(String aucResult) {
String allChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
String allNum = "0123456789";
StringBuilder stb = new StringBuilder();
char temp;
for (int i = 0; i < aucResult.length(); i++) {
if (aucResult.charAt(i) >= 'A' && aucResult.charAt(i) <= 'Z') {
if (aucResult.charAt(i) != 'A') {
int flag = (int) (aucResult.charAt(i) - 66);
temp = allChar.charAt(flag);
stb.append(temp);
} else {
stb.append('z');
}
}
if (aucResult.charAt(i) >= 'a' && aucResult.charAt(i) <= 'z') {
if (aucResult.charAt(i) != 'a') {
int flag = (int) (aucResult.charAt(i) - 72);
temp = allChar.charAt(flag);
stb.append(temp);
} else {
stb.append('Z');
}
}
if (aucResult.charAt(i) >= '0' && aucResult.charAt(i) <= '9') {
if (aucResult.charAt(i) != '0') {
int flag = (int) (aucResult.charAt(i) - 49);
temp = allNum.charAt(flag);
stb.append(temp);
} else {
stb.append('9');
}
}
}
return stb.toString();
}
}