题解 | #蛇形矩阵#

字符串加密

http://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3

import java.util.*;

public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()) { String encryCode = sc.nextLine(); String encode = solution(encryCode); // System.out.println(encode); String code = sc.nextLine(); // dump duplicate char encryString(encode, code); } }

public static String solution(String someString) {
    StringBuilder str = new StringBuilder();
    String test = someString.toLowerCase();
    String encode = "abcdefghijklmnopqrstuvwxyz";
    for(int i = 0; i < test.length(); i++) {
        if(!str.toString().contains(test.charAt(i)+"")) {
            str.append(test.charAt(i));
        }
    }
    
    for(int i = 0; i < encode.length(); i++) {
        if(!str.toString().contains(encode.charAt(i)+"")) {
            str.append(encode.charAt(i));
        }
    }
    return str.toString();
}

public static void encryString(String encode, String code) {
    String[] test = code.split("\\s");
    String[] result = new String[test.length];
    
    for(int i = 0; i < test.length; i++) {
        StringBuilder str = new StringBuilder();
        for(int j = 0; j < test[i].length(); j++) {
            if(test[i].charAt(j) >= 'A' && test[i].charAt(j) <= 'Z') {
                String a = test[i].charAt(j)+"";
                str.append((encode.charAt(a.toLowerCase().charAt(0) - 'a')+"").toUpperCase());
            } else {
                str.append(encode.charAt(test[i].charAt(j) - 'a')+"");
            }
        }
        result[i]= str.toString();
    }
    for(int i = 0; i < result.length; i++) {
        System.out.println(result[i]);
    }
}

}

// Sorry I am late, but this is so easy, every body who want to known, what happen, just tell me, thank you very much, and good night

全部评论

相关推荐

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