题解 | #简单密码#

简单密码

https://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Pattern;
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        String result = "";
        if (str.length() <= 100) {
            Pattern partternLowerStr = Pattern.compile("[a-z]");
            Pattern partternUpperStr = Pattern.compile("[A-Z]");
            String[] strName = {"a-2", "b-2", "c-2", "d-3", "e-3", "f-3", "g-4", "h-4", "i-4", "j-5", "k-5", "l-5",
                                "m-6", "n-6", "o-6", "p-7", "q-7", "r-7", "s-7", "t-8", "u-8", "v-8", "w-9", "x-9",
                                "y-9", "z-9"
                               };
            for (int i = 0; i < str.length(); i++) {
                String c = String.valueOf(str.charAt(i));
                if (partternLowerStr.matcher(c).matches()) {
                    for (int j = 0; j < strName.length; j++) {
                        String name = strName[j].split("-")[0];
                        if (name.equals(c)) {
                            c = strName[j].split("-")[1];
                            break;
                        }
                    }
                }
                if (partternUpperStr.matcher(c).matches()) {
                    int index = 0;
                    for (int j = 0; j < strName.length; j++) {
                        String name = strName[j].split("-")[0];
                        if (name.equals(c.toLowerCase())) {
                            index = j;
                            break;
                        }
                    }
                    c = strName[index == strName.length - 1 ? 0 : index + 1].split("-")[0];
                }
                result += c;
            }
            System.out.print(result);
        }
    }
}
#华为#
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务