规定这样一种密码的加密方法:
对于密码中的英文字母,按照字母表顺序,向后移动一位,同时改变大小写,即 转换为 , 转换为 , 转换为 , , 转换为 , 转换为 。
对于密码中的数字,增加 , 转换为 。
字符串的解密方法即为加密方法的逆过程。
现在,对于给定的明文字符串 ,将其加密;对于给定的密文字符串 ,将其解密。
第一行输入一个长度为 的字符串 ,代表给定的明文字符串;
第二行输入一个长度为 的字符串 ,代表给定的密文字符串。
除此之外,保证字符串 和 中仅包含英文字母和数字。
第一行输出一个字符串,代表加密后的 。
第二行输出一个字符串,代表解密后的 。
abcdefg1 0BCDEFGH
BCDEFGH2 9abcdefg
public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String line = sc.nextLine(); String line1 = sc.nextLine(); char[] chars = line.toCharArray(); char[] chars1 = line1.toCharArray(); System.out.println(encipher(chars)); System.out.println(decrypt(chars1)); } } // 加密 private static StringBuilder encipher(char[] chars) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < chars.length; i++) { if (Character.isLetter(chars[i])) { if (Character.isUpperCase(chars[i])) { // 转小写 char toLowerCase = Character.toLowerCase(chars[i]); builder.append(Character.isLetter((char)(toLowerCase + 1))?(char)(toLowerCase + 1):"a"); } else { // 转大写 char toUpperCase = Character.toUpperCase(chars[i]); builder.append(Character.isLetter((char)(toUpperCase + 1))?(char)(toUpperCase + 1):"A"); } } else { Integer integer = Integer.valueOf(chars[i])-48; builder.append((integer + 1) % 10); } } return builder; } // 解密 private static StringBuilder decrypt(char[] chars) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < chars.length; i++) { if (Character.isLetter(chars[i])) { if (Character.isUpperCase(chars[i])) { char toLowerCase = Character.toLowerCase(chars[i]); builder.append(Character.isLetter((char)(toLowerCase - 1))?(char)(toLowerCase - 1):"z"); } else { char toUpperCase = Character.toUpperCase(chars[i]); builder.append(Character.isLetter((char)(toUpperCase - 1))?(char)(toUpperCase - 1):"Z"); } } else { Integer integer = Integer.valueOf(chars[i])-48; builder.append((integer - 1) % 10 < 0 ? 9 : (integer - 1) % 10); } } return builder; }
import java.util.Scanner; import java.lang.Character; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String text = in.next(); String code = in.next(); System.out.println(encode(text)); System.out.println(decode(code)); } public static String encode(String text) { String code = ""; for (char c : text.toCharArray()) { if (Character.isDigit(c)) { code += (c + 3) / 10 == 5 ? (char)(c + 1) : '0'; } else if (Character.isLowerCase(c)) { code += (c + 8) / 26 == 4 ? (char)(c - 31) : 'A'; } else if (Character.isUpperCase(c)) { code += (c + 14) / 26 == 3 ? (char)(c + 33) : 'a'; } else { code += c; } } return code; } public static String decode(String code) { String text = ""; for (char c : code.toCharArray()) { if (Character.isDigit(c)) { text += (c + 2) % 10 > 0 ? (char)(c - 1) : '9'; } else if (Character.isLowerCase(c)) { text += (c + 7) % 26 > 0 ? (char)(c - 33) : 'Z'; } else if (Character.isUpperCase(c)) { text += (c + 13) % 26 > 0 ? (char)(c + 31) : 'z'; } else { text += c; } } return text; } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String one = in.nextLine(); char[] oneArr = one.toCharArray(); String two = in.nextLine(); char[] twoArr = two.toCharArray(); StringBuffer enc = new StringBuffer(); StringBuffer dec = new StringBuffer(); for (char c : oneArr) { enc.append(convert(c,1)); } for (char c : twoArr) { dec.append(convert(c,2)); } System.out.println(enc); System.out.println(dec); } } private static char convert(char a, int type) { if (type == 1){ if (a >= 'a' && a < 'z') { return (char) (a - 31); } if (a >= 'A' && a < 'Z') { return (char) (a + 33); } if (a >= '0' && a < '9') { return (char) (a + 1); } if (a == 'z') { return 'A'; } if (a == 'Z') { return 'a'; } if (a == '9') { return '0'; } }else { if (a > 'a' && a <= 'z') { return (char) (a - 33); } if (a > 'A' && a <= 'Z') { return (char) (a + 31); } if (a > '0' && a <= '9') { return (char) (a - 1); } if (a == 'a') { return 'Z'; } if (a == 'A') { return 'z'; } if (a == '0') { return '9'; } } return a; } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); char[] in1 = in.nextLine().toCharArray(); char[] in2 = in.nextLine().toCharArray(); String src = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; String code = "bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA1234567890"; StringBuilder sb = new StringBuilder(); for (int i = 0; i < in1.length; i++) { sb.append(code.charAt(src.indexOf(in1[i]))); } System.out.println(sb); sb.setLength(0); for (int i = 0; i < in2.length; i++) { sb.append(src.charAt(code.indexOf(in2[i]))); } System.out.println(sb); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s1 = in.nextLine(); String s2 = in.nextLine(); StringBuilder encoding = new StringBuilder(); StringBuilder decoding = new StringBuilder(); for(int i = 0; i < s1.length(); i++){ char c = s1.charAt(i); if( c >= 'a' && c <= 'z'){ encoding.append((char)('A'+(c - 'a' + 1) % 26)); }else if ( c >= 'A' && c <= 'Z'){ encoding.append((char)('a'+(c - 'A' + 1) % 26)); }else{ encoding.append((char)('0'+(c - '0' + 1) % 10)); } } for(int i = 0; i < s2.length(); i++){ char c = s2.charAt(i); if( c >= 'a' && c <= 'z'){ decoding.append((char)('Z'+(c - 'z' - 1) % 26)); }else if ( c >= 'A' && c <= 'Z'){ decoding.append((char)('z'+(c - 'Z' - 1) % 26)); }else{ decoding.append((char)('9'+(c - '9' - 1) % 10)); } } System.out.println(new String(encoding)); System.out.print(new String(decoding)); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case char[] zip = in.nextLine().toCharArray(); char[] unzip = in.nextLine().toCharArray(); for (int i = 0; i < zip.length; i ++) { zip[i] = change(zip[i], 1); } for (int i = 0; i < unzip.length; i ++) { unzip[i] = change(unzip[i], -1); } System.out.println(new String(zip)); System.out.println(new String(unzip)); } } public static char change(char c, int k) { if (Character.isLowerCase(c)) { int val = (c - 'a' + k + 26) % 26; return (char)('A' + val); } else if (Character.isUpperCase(c)) { int val = (c - 'A' + k + 26) % 26; return (char)('a' + val); } else { int val = (c - '0' + k + 10) % 10; return (char)('0' + val); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String arr[]=new String [2]; int index=0; while(in.hasNextLine()){ arr[index]=in.nextLine(); index++; } for(int i=0;i<arr[0].length();i++){ if(i<arr[0].length()-1){ if(arr[0].charAt(i)>='A'&&arr[0].charAt(i)<'Z'){ int num=((int)arr[0].charAt(i))+33; System.out.print((char)num); }else if(arr[0].charAt(i)=='Z'){ System.out.print("a"); }else if(arr[0].charAt(i)>='a'&&arr[0].charAt(i)<'z'){ int num=((int)arr[0].charAt(i))-31; System.out.print((char)num); }else if(arr[0].charAt(i)=='z') { System.out.print("A"); }else if(arr[0].charAt(i)>='0'&&arr[0].charAt(i)<'9'){ int num=((int)arr[0].charAt(i))+1; System.out.print((char)num); }else if(arr[0].charAt(i)=='9') { System.out.print("0"); } }else { if(arr[0].charAt(i)>='A'&&arr[0].charAt(i)<'Z'){ int num=((int)arr[0].charAt(i))+33; System.out.println((char)num); }else if(arr[0].charAt(i)=='Z'){ System.out.println("a"); }else if(arr[0].charAt(i)>='a'&&arr[0].charAt(i)<'z'){ int num=((int)arr[0].charAt(i))-31; System.out.println((char)num); }else if(arr[0].charAt(i)=='z') { System.out.println("A"); }else if(arr[0].charAt(i)>='0'&&arr[0].charAt(i)<'9'){ int num=((int)arr[0].charAt(i))+1; System.out.println((char)num); }else if(arr[0].charAt(i)=='9') { System.out.println("0"); } } } for(int i=0;i<arr[1].length();i++) { if (arr[1].charAt(i) > 'A' && arr[1].charAt(i) <= 'Z') { int num = ((int) arr[1].charAt(i)) + 31; System.out.print((char) num); } else if (arr[1].charAt(i) == 'A') { System.out.print("z"); } else if (arr[1].charAt(i) > 'a' && arr[1].charAt(i) <= 'z') { int num = ((int) arr[1].charAt(i))-33; System.out.print((char) num); } else if (arr[1].charAt(i) == 'a') { System.out.print("Z"); } else if (arr[1].charAt(i) > '0' && arr[1].charAt(i) <= '9') { int num = ((int) arr[1].charAt(i)) - 1; System.out.print((char) num); } else if (arr[1].charAt(i) == '0') { System.out.print("9"); } } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String encode = sc.next(); String decode = sc.next(); encode(encode); decode(decode); } public static void encode(String encode) { char[] chars = encode.toCharArray(); for (int i = 0; i < chars.length; i++) { if (Character.isUpperCase(chars[i])) { chars[i] = chars[i] == 'Z' ? 'a' : (char) (chars[i] + 1 + 32); } else if (Character.isLowerCase(chars[i])) { chars[i] = chars[i] == 'z' ? 'A' : (char) (chars[i] + 1 - 32); } else if (Character.isDigit(chars[i])) { chars[i] = chars[i] == '9' ? '0' : (char) (chars[i] + 1); } } System.out.println(String.valueOf(chars)); } public static void decode(String decode) { char[] chars = decode.toCharArray(); for (int i = 0; i < chars.length; i++) { if (Character.isUpperCase(chars[i])) { chars[i] = chars[i] == 'A' ? 'z' : (char) (chars[i] - 1 + 32); } else if (Character.isLowerCase(chars[i])) { chars[i] = chars[i] == 'a' ? 'Z' : (char) (chars[i] - 1 - 32); } else if (Character.isDigit(chars[i])) { chars[i] = chars[i] == '0' ? '9' : (char) (chars[i] - 1); } } System.out.println(String.valueOf(chars)); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String a = in.nextLine(); String b = in.nextLine(); System.out.println(encript(a)); System.out.println(decript(b)); } } public static String encript(String str){ char[] data=str.toCharArray(); for(int i=0;i<data.length;i++){ char t=data[i]; if(Character.isLetter(t)){ if(t=='z'){ data[i]='A'; }else if(t=='Z'){ data[i]='a'; }else{ data[i]=(char)((t+1)^' '); } }else{ if(t=='9'){ data[i]='0'; }else{ data[i]=(char)(t+1); } } } return String.valueOf(data); } public static String decript(String str){ char[] data=str.toCharArray(); for(int i=0;i<data.length;i++){ char t=data[i]; if(Character.isLetter(t)){ if(t=='a'){ data[i]='Z'; }else if(t=='A'){ data[i]='z'; }else{ data[i]=(char)((t-1)^' '); } }else{ if(t=='0'){ data[i]='9'; }else{ data[i]=(char)(t-1); } } } return String.valueOf(data); } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str1 = in.nextLine(); String str2 = in.nextLine(); //使用字典法,简洁明了 String a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; String b = "BCDEFGHIJKLMNOPQRSTUVWXYZAbcdefghijklmnopqrstuvwxyza1234567890"; StringBuilder out1 = new StringBuilder(); StringBuilder out2 = new StringBuilder(); for (int i = 0; i < str1.length(); i++) { out1.append(b.charAt(a.indexOf(str1.charAt(i)))); } for (int i = 0; i < str2.length(); i++) { out2.append(a.charAt(b.indexOf(str2.charAt(i)))); } System.out.println(out1); System.out.println(out2); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String plaintext = in.next(); String ciphertext = in.next(); System.out.println(encrypt(plaintext.toCharArray())); System.out.println(decrypt(ciphertext.toCharArray())); } static char[] encrypt(char[] charArray) { for (int i = 0; i < charArray.length; i++) { if (charArray[i] >= 'a' && charArray[i] <= 'z') { charArray[i] -= 'a'; charArray[i] = (char) ((charArray[i] + 1) % 26); charArray[i] += 'A'; } else if (charArray[i] >= 'A' && charArray[i] <= 'Z') { charArray[i] -= 'A'; charArray[i] = (char) ((charArray[i] + 1) % 26); charArray[i] += 'a'; } else if (charArray[i] >= '0' && charArray[i] <= '9') { charArray[i] -= '0'; charArray[i] = (char) ((charArray[i] + 1) % 10); charArray[i] += '0'; } } return charArray; } static char[] decrypt(char[] charArray) { for (int i = 0; i < charArray.length; i++) { if (charArray[i] >= 'a' && charArray[i] <= 'z') { charArray[i] -= 'a'; charArray[i] = (char) ((charArray[i] + 25) % 26); charArray[i] += 'A'; } else if (charArray[i] >= 'A' && charArray[i] <= 'Z') { charArray[i] -= 'A'; charArray[i] = (char) ((charArray[i] + 25) % 26); charArray[i] += 'a'; } else if (charArray[i] >= '0' && charArray[i] <= '9') { charArray[i] -= '0'; charArray[i] = (char) ((charArray[i] + 9) % 10); charArray[i] += '0'; } } return charArray; } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 String str1 = in.nextLine(); String str2 = in.nextLine(); String result1 = ""; String result2 = ""; //加密 for (int i = 0; i < str1.length(); i++) { char tmp = str1.charAt(i); //字符 if (Character.isLetter(str1.charAt(i))) { if (tmp == 'z') { tmp = 'a'; } else if (tmp == 'Z') { tmp = 'A'; } else { tmp = (char)(tmp + 1); } if (tmp >= 'A' && tmp <= 'Z') { tmp = (tmp + "").toLowerCase().charAt(0); }else if (tmp >= 'a' && tmp <= 'z') { tmp = (tmp + "").toUpperCase().charAt(0); } //数字 }else if(Character.isDigit(tmp)){ if(tmp!='9'){ tmp=(char)(tmp+1); }else{ tmp='0'; } } result1 += tmp; } //解密 for (int i = 0; i < str2.length(); i++) { char tmp = str2.charAt(i); //字符 if (Character.isLetter(str2.charAt(i))) { if (tmp == 'a') { tmp = 'z'; } else if (tmp == 'A') { tmp = 'Z'; } else { tmp = (char)(tmp -1); } if (tmp >= 'A' && tmp <= 'Z') { tmp = (tmp + "").toLowerCase().charAt(0); }else if (tmp >= 'a' && tmp <= 'z') { tmp = (tmp + "").toUpperCase().charAt(0); } //数字 }else if(Character.isDigit(tmp)){ if(tmp!='0'){ tmp=(char)(tmp-1); }else{ tmp='9'; } } result2 += tmp; } System.out.println(result1); System.out.print(result2); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String a = in.nextLine(); String b = in.nextLine(); //加密后的字符 StringBuffer encrypt = encrypt(a); System.out.println(encrypt); //解密后的字符 StringBuffer decode = decode(b); System.out.println(decode); } public static StringBuffer encrypt(String a) { StringBuffer buf = new StringBuffer(); for (int i = 0; i < a.length(); i++) { char c = a.charAt(i); String lt = String.valueOf(c); if (lt.matches(".*[a-z]")) { if (lt.equals("z")) { buf.append("A"); } else { char alt = (char)(a.charAt(i) + 1); buf.append(String.valueOf(alt).toUpperCase()); } } if (lt.matches(".*[A-Z]")) { if (lt.equals("Z")) { buf.append("a"); } else { char alt = (char)(a.charAt(i) + 1); buf.append(String.valueOf(alt).toLowerCase()); } } if (lt.matches(".*[0-9]")) { if (lt.equals("9")) { buf.append("0"); } else { int i1 = Integer.valueOf(String.valueOf(c)) + 1; buf.append(i1); } } } return buf; } public static StringBuffer decode(String a) { StringBuffer buf = new StringBuffer(); for (int i = 0; i < a.length(); i++) { char c = a.charAt(i); String lt = String.valueOf(c); if (lt.matches(".*[a-z]")) { if (lt.equals("a")) { buf.append("Z"); } else { char alt = (char)(a.charAt(i) - 1); buf.append(String.valueOf(alt).toUpperCase()); } } if (lt.matches(".*[A-Z]")) { if (lt.equals("A")) { buf.append("z"); } else { char alt = (char)(a.charAt(i) - 1); buf.append(String.valueOf(alt).toLowerCase()); } } if (lt.matches(".*[0-9]")) { if (lt.equals("0")) { buf.append("9"); } else { int i1 = Integer.valueOf(String.valueOf(c)) - 1; buf.append(i1); } } } return buf; } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case String a = in.nextLine(); String b = in.nextLine(); StringBuilder up = new StringBuilder(); StringBuilder low = new StringBuilder(); StringBuilder num = new StringBuilder(); int lengthA = a.length(); int lengthB = b.length(); // 输出加密字符串 for (int i = 0; i < lengthA; i++) { char c = a.charAt(i); if (Character.isUpperCase(c)) { // up.append(Character.toLowerCase(c)); c = (char) (c + 1); c = c > 'Z' ? 'A' : c; c = Character.toLowerCase(c); System.out.printf("" + c); } else if (Character.isLowerCase(c)) { // low.append(Character.toUpperCase(c)); c = (char) (c + 1); c = c > 'z' ? 'a' : c; c = Character.toUpperCase(c); System.out.printf("" + c); } else { // num.append(c == '9' ? '0' : (c + 1)); c = (char) (c + 1); if (c > '9') { c = '0'; } System.out.printf("" + c); } } System.out.printf("\n"); // 输出解密字符串 for (int i = 0; i < lengthB; i++) { char c = b.charAt(i); if (Character.isUpperCase(c)) { // up.append(Character.toLowerCase(c)); c = (char) (c - 1); c = c < 'A' ? 'Z' : c; c = Character.toLowerCase(c); System.out.printf("" + c); } else if (Character.isLowerCase(c)) { // low.append(Character.toUpperCase(c)); c = (char) (c - 1); c = c < 'a' ? 'z' : c; c = Character.toUpperCase(c); System.out.printf("" + c); } else { // num.append(c == '9' ? '0' : (c + 1)); c = (char) (c - 1); if (c < '0') { c = '9'; } System.out.printf("" + c); } } System.out.printf("\n"); } } }
算了,还是这样吧!
简单明了,不需要多思考
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { System.out.println(encodeString(scanner.next())); System.out.println(decodeString(scanner.next())); } } public static String encodeString(String string) { char[] chars = string.toCharArray(); StringBuilder builder = new StringBuilder(); for (char aChar : chars) { char next = (char) (aChar + 1); if (Character.isLowerCase(aChar)) { if (aChar == 'z') { builder.append('A'); continue; } builder.append(Character.toUpperCase(next)); } if (Character.isUpperCase(aChar)) { if (aChar == 'Z') { builder.append('a'); continue; } builder.append(Character.toLowerCase(next)); } if (Character.isDigit(aChar)) { if (aChar == '9') { builder.append('0'); continue; } builder.append(next); } } return builder.toString(); } public static String decodeString(String string) { char[] chars = string.toCharArray(); StringBuilder builder = new StringBuilder(); for (char aChar : chars) { char next = (char) (aChar - 1); if (Character.isLowerCase(aChar)) { if (aChar == 'a') { builder.append('Z'); continue; } builder.append(Character.toUpperCase(next)); } if (Character.isUpperCase(aChar)) { if (aChar == 'A') { builder.append('z'); continue; } builder.append(Character.toLowerCase(next)); } if (Character.isDigit(aChar)) { if (aChar == '0') { builder.append('9'); continue; } builder.append(next); } } return builder.toString(); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String enCode = in.nextLine(); String deCode = in.nextLine(); System.out.println(code(true, enCode)); System.out.println(code(false, deCode)); } public static String code (boolean isCode, String str) { char[] strChar = str.toCharArray(); int shift = -1; if (isCode) { shift = 1; } for (int i = 0; i < strChar.length; i ++) { if (Character.isLetter(strChar[i])) { int charShift = 1; if (Character.isUpperCase(strChar[i])) { charShift = -1; } if ((strChar[i] == 'Z' || strChar[i] == 'z') && shift == 1) { strChar[i] = charShift == -1 ? 'a' : 'A'; } else if ((strChar[i] == 'A' || strChar[i] == 'a') && shift == -1) { strChar[i] = charShift == -1 ? 'z' : 'Z'; } else { strChar[i] = (char) (strChar[i] + charShift * ('A' - 'a') + shift * 1); } } else { strChar[i] = (char) ((strChar[i] + shift + 10 - '0') % 10 + '0'); } } return new String(strChar); } }