字符串加解密
字符串加解密
http://www.nowcoder.com/questionTerminal/2aa32b378a024755a3f251e75cbf233a
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNextLine()){ String s1 = sc.nextLine(); String s2 = sc.nextLine(); encode(s1); decode(s2); } } // 解密 public static void encode(String s){ char[] cc = s.toCharArray(); for(int i = 0; i < s.length(); ++i){ if(cc[i] >= 'a' && cc[i] <= 'z'){ int k = cc[i] - 'a'; System.out.print((char)(('a' + (k + 1)%26) - 32)); }else if(cc[i] >= 'A' && cc[i] <= 'Z'){ int k = cc[i] - 'A'; System.out.print((char)(('A' + (k + 1)%26) + 32)); }else{ int k = cc[i] - '0'; System.out.print((char)('0' + (k + 1)%10)); } } System.out.println(); } // 解密 public static void decode(String s){ char[] cc = s.toCharArray(); for(int i = 0; i < s.length(); ++i){ if(cc[i] >= 'a' && cc[i] <= 'z'){ int k = cc[i] - 'a'; System.out.print((char)(('a' + (k + 26 - 1)%26) - 32)); }else if(cc[i] >= 'A' && cc[i] <= 'Z'){ int k = cc[i] - 'A'; System.out.print((char)(('A' + (k + 26 - 1)%26) + 32)); }else{ int k = cc[i] - '0'; System.out.print((char)('0' + (k + 10 - 1)%10)); } } System.out.println(); } }