题解 | #简单密码#
简单密码
https://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readLine(); StringBuffer sb = new StringBuffer(); // 小写字母映射数组 int[] map = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9}; for(char c : str.toCharArray()){ if(Character.isDigit(c)){ sb.append(c); } else if(Character.isLetter(c)){ if (c >= 97 && c <= 122){ // 映射小写字母 int s = map[c - 'a']; sb.append(s); } else if(c >= 65 && c <= 89){ // 映射大写字母,除了Z c = Character.toLowerCase(c); c++; sb.append(c); } else { // 映射大写字母Z sb.append('a'); } } } System.out.println(sb.toString()); } }