题解 | #字符串合并处理#

字符串合并处理

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

那就打表呗。。。

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Map<Character,Character> map = new HashMap<>();
//  0    1    2   3     4     5   6    7   8     9   a    b    c    d     e   f
//0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
//  0    8   4    C   2    A     6    E   1    9    5    D    3    B     7    F
        map.put('1','8');
        map.put('2','4');
        map.put('3', 'C');
        map.put('4', '2');
        map.put('5', 'A');
        map.put('6', '6');
        map.put('7', 'E');
        map.put('8', '1');
        map.put('9', '9');
        map.put('a', '5');
        map.put('b', 'D');
        map.put('c', '3');
        map.put('d', 'B');
        map.put('e', '7');
        map.put('f', 'F');
        map.put('A','5');
        map.put('B','D');
        map.put('C','3');
        map.put('D','B');
        map.put('E','7');
        map.put('F','F');
        while (in.hasNext()) { 
           String str1 = in.nextLine();
           String[] strs = str1.split(" ");
           String str = strs[0]+strs[1];
           int len = str.length();
           int len1 = len/2;
           int len2 = len/2;
           if(len%2==1)len1++;
           char[] ch1 = new char[len1];
           char[] ch2 = new char[len2];
           for(int i=0;i<len;++i){
                if(i%2==0)
                    ch1[i/2] = str.charAt(i);
                else ch2[i/2] = str.charAt(i);
            }
            Arrays.sort(ch1);
            Arrays.sort(ch2);
            String res = "";
            String tempstr = "";
            for(int i =0;i<len;++i){
                if(i%2==0){
                    res+=map.get(ch1[i/2])==null?ch1[i/2]:map.get(ch1[i/2]);
                }
                else res+=map.get(ch2[i/2])==null?ch2[i/2]:map.get(ch2[i/2]);
            }
             
            System.out.println(res);
        }
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-10 11:33
点赞 评论 收藏
分享
06-08 22:25
门头沟学院 Java
从零开始的转码生活:这hr不会打开手机不分青红皂白给所有人群发这句话,过一会再给所有人再发一遍,这肯定会有重复的,不管,再过一会再发一遍
点赞 评论 收藏
分享
码农索隆:有点耳熟,你们是我教过最差的一届
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务