题解 | #简单密码#

简单密码

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

import java.util.*;

public class Main {

private static Map<String, Integer> map = new HashMap<>();

static {
    map.put("1", 1);
    map.put("abc", 2);
    map.put("def", 3);
    map.put("ghi", 4);
    map.put("jkl", 5);
    map.put("mno", 6);
    map.put("pqrs", 7);
    map.put("tuv", 8);
    map.put("wxyz", 9);
    map.put("0", 0);
}
    
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    while (sc.hasNext()){
        String newPwd = "";
        String oldPwd = sc.nextLine();
        char[] chars = oldPwd.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if(chars[i]>= 'A' && chars[i]<='Z'){
                newPwd = newPwd + bigToSmall(chars[i]);
            }else if(chars[i]>='a' && chars[i]<='z'){
                newPwd = newPwd + smallToNum(chars[i]);
            }else {
                newPwd = newPwd + chars[i];
            }
        }
        System.out.println(newPwd);
    }
}

private static String bigToSmall(char c){
    String big = c+"";
    String small = "";
    if("Z".equals(big)){
        return "a";
    }
    char[] chars = big.toCharArray();
    char charSmall = (char) (chars[0]+1);
    small = (charSmall+"").toLowerCase();
    return small;
}

private static String smallToNum(char c){
    String small = c+"";
    Set<String> keySet = map.keySet();
    Iterator<String> iterator = keySet.iterator();
    while (iterator.hasNext()){
        String key = iterator.next();
        if(key.contains(small)){
            return map.get(key)+"";
        }
    }
    return small;
}

}

全部评论

相关推荐

10-13 17:47
门头沟学院 Java
wulala.god:图一那个善我面过,老板网上找的题库面的
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务