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

字符串合并处理

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);
        }
    }
}
全部评论

相关推荐

今天 14:00
门头沟学院 运营
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-09 12:11
点赞 评论 收藏
分享
05-29 09:02
门头沟学院 Java
点赞 评论 收藏
分享
强大的马里奥:不太可能,我校计算机硕士就业率99%
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-09 12:02
ssob上原来真有BOSS啊
硫蛋蛋:这种也是打工的,只不是是给写字楼房东打工
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

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